Subtracting binary numbers using two's complement

﹥>﹥吖頭↗ 提交于 2019-12-12 07:03:07

问题


Assume 151 and 214 are signed 8-bit decimal integers stored in two’s complement format. Calculate 151 - 214 using saturating arithmetic. The result should be written in decimal. Show your work.

Is this right? I am new to this:

decimal | binary | twos complement
151 | 10010111 | 01101001
214 | 11010110 | 00101010

subtract 00111111 -> 63

Why do I get positive 63?


回答1:


"Two's complement format" is the normal binary representation of signed integers. It doesn't mean that you have to perform the two's complement operation.

That said, this question is hard to parse. "Assume that 151 is an 8-bit signed integer" is silly, since it's too big to be an 8-bit signed integer. This probably means that you're supposed to re-interpret the (unsigned) binary representations of 151 and 214 as 8-bit signed representations, and then subtract the numbers that result:

When the representations of 151 and 214 are interpreted as signed, the bits in the 128 place become -128 instead. The difference is 256, so 151 becomes 151-256 = -105, and 214 becomes -42.

If we calculate -105 - (-42), we get -63. That is the same as 151-214, which is maybe the point of this question.

This is small enough not to saturate, so it doesn't matter in this case that we're using saturating arithmetic.

Normally I wouldn't answer a homework question like this, but I feel sorry that the question you have to answer is so badly written. You should probably delete the question instead of accepting this answer.



来源:https://stackoverflow.com/questions/57846917/subtracting-binary-numbers-using-twos-complement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!