Two's complement is a mathematical operation on binary numbers, best known for its role in computing as a method of signed number representation.

Before we talk about principle, we need to proof it correct:

So it's right. Why it works?

Given a set of all possible N-bit values, we can assign the lower (by the binary value) half to be the integers from 0 to (2N − 1 − 1) inclusive and the upper half to be −2N − 1 to −1 inclusive. The upper half (again, by the binary value) can be used to represent negative integers from −2N − 1 to −1 because, under addition modulo 2N they behave the same way as those negative integers. That is to say that because i + j mod 2N = i + (j + 2N) mod 2N any value in the set { j + k 2N | k is an integer }  can be used in place of j.

ReferenceLink

 

分类: 计算机科学

1 条评论

rama · 2022年7月4日 上午6:24

I have read so many posts about the blogger lovers however this post is really a good piece of writing, keep it up

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注