Information Representation
Inside a computer, everything is bits. The topic of information representation discusses how different information inside a computer program actually looks like in the background.
Signed Integers
While a series of bits naturally represent a binary number, negative numbers are a bit more complicated.
The first method to represent signed integers with bits is signed magnitude. Under this system, the first bit (sign bit) is used as a negative sign; If it is 0, then the number is positive, otherwise it is negative.
The second method is called 2's complement, and it is more widely used due to several advantages covered later.
In this system, think of the first bit in the bit pattern as negative. For example, 101 would be 4 + 1 = 5 as an unsigned number, but -4 + 1 = -3 as a signed number represented by 2's complement.
The advantage of using 2's complement over signed magnitude is twofold:
- Normal binary arithmetic can be applied.
- There is no negative zero
To negate signed numbers, flip all bits and add 1. This comes from math: