无符号数和二进制补码有符号数的哪些算术运算相同? [英] Which arithmetic operations are the same on unsigned and two's complement signed numbers?

查看:30
本文介绍了无符号数和二进制补码有符号数的哪些算术运算相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个简单的玩具指令集和附带的模拟器,并且我正在尝试找出支持哪些指令.在算术方面,我目前有无符号加减乘除.但是,我似乎无法找到以下问题的明确答案:哪些算术运算符需要有符号版本,哪些是无符号和二进制补码有符号版本?

I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent?

因此,例如,二进制补码中的 1111 等于 -1.如果你给它加 1 并假装它是一个无符号数,你会得到 0000,即使认为它是 -1 也是正确的.但是,这适用于所有数字吗?那么其他三个操作(减法、乘法、除法)呢?

So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking of it as -1. However, does that hold for all numbers? And what about for the other three operations (subtraction, multiplication, division)?

推荐答案

加减法对于有符号和无符号的 2s 补码是相同的,假设您要以大多数 CPU 的正常方式处理溢出/下溢,即只是环绕.乘法和除法是不同的.所以你只需要一个加法例程和一个减法例程而不管有符号性,但你需要单独的有符号和无符号乘法和除法.

Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.

这篇关于无符号数和二进制补码有符号数的哪些算术运算相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆