Two's Complement
Explore how two's complement represents positive and negative integers in Bash programming, enabling efficient single-step CPU additions and unique zero representation. Understand its advantages over one's complement, see examples of binary addition with overflow handling, and practice converting and adding numbers in this format.
How two’s complement solves problems
The two’s complement solves both problems of SMR. First, it allows the CPU adder to operate negative numbers in one step. We need two steps for that when using the one’s complement. Second, the two’s complement has only one way to represent zero.
Positive integers in the two’s complement and SMR look the same. The highest-order bit equals zero. The remaining bits store the number.
Negative integers in the two’s complement have the highest-order bit equal to one. The rest of the bits are inverted the same way as in the one’s complement. The only difference is that we need to add one to the negative number after inverting its bits.
The following table shows the two’s complement representation of a few ...