signed binary numbers jbwyatt.com
Using the unsigned binary number system we know: 1 is 2^0 = 1 10 is 2^1 = 2 100 is 2^2 = 4 ... 1000 0000 is 2^7 = 128
We know the place values for 8 bits UNSIGNED are: 128 64 32 16 8 4 2 1 --- -- -- -- - - - - 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 SO: 1 0 0 0 0 0 1 0 = 128 + 2 = 130

To express negative numbers, the scheme of the binary positional number system is simply changed slightly - with a large impact. Using n bits, the nth bit is simply changed from positive to negative. The place values for n=8 bits SIGNED are: -128 64 32 16 8 4 2 1 --- -- -- -- - - - - -2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 SO: 1 0 0 0 0 0 1 0 = -128 + 2 = -126
Numbers using this scheme can be negated by taking the two's complement of the number - flipping the bits and adding one. 0000 1111 = 15 = 8 + 4 + 2 + 1 1111 0001 = -15 = -128 + 64 + 32 + 16 + 1 (flip all bits and add one)
The high bit, (nth it) becomes the "sign bit" only because, if set, it indicates the number must be negative - it actually represents -2^(n-1).
Binary Decimal Unsigned Decimal Signed
a. 1111 1111 255 -1
b. 0000 0000 0 0
c. 1000 0000 128 -128
d. 0111 1111 127 127
e. 1000 0001 129 -127 (note: flip bits and add 1 from d. above)
f. 1000 1111 143 -113
g. 0000 1111 15 15
h. 1111 0001 241 -15 (note: flip bits and add 1 from g. above)
n > 8 bits ========== n = 16 ====== Nothing changes - an elegant scheme. If n = 16, the nth bit (2^(n-1)) becomes -32,768 -32768 .. 128 64 32 16 8 4 2 1 ----- --- -- -- -- - - - - 2^15 .. 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 SO: 1 .. 0 0 0 0 0 0 1 0 = -32768 + 2 = -32766 n = 32 ====== If n = 32, the nth bit (2^(n-1)) becomes -2,147,483,648 -------------------------------------- Interestingly, n '1' bits (111..111) is ALWAYS -1 decimal. Why?
valid xhtml 1.0