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

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

问题描述

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

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天全站免登陆