signed integers in binary

UNSIGNED INTEGERS

Using the binary number system we can express unsigned integers: 12 is 20 = 110 102 is 21 = 210 1012 is 22 + 20 = 4 + 1 = 510 ... 1000 00002 is 27 = 12810 We know the place values for 8 bits UNSIGNED are: 128 64 32 16 8 4 2 1 --- -- -- -- - - - - 27 26 25 24 23 22 21 20 So: 1 0 0 0 1 0 1 0 = 128 + 8 + 2 = 13810 We also know this number can be represented in hex as 8A16 and in octal as 2128. We also know that we can represent 256 integers in 8 bits and the largest integer is 255.

SIGNED INTEGERS

There are multiple schemes to represent negative numbers including: Sign Magnitude One's Complement Two's Complement 000 = +0 000 = +0 000 = +0 001 = +1 001 = +1 001 = +1 010 = +2 010 = +2 010 = +2 011 = +3 011 = +3 011 = +3 100 = -0 100 = -3 100 = -4 101 = -1 101 = -2 101 = -3 110 = -2 110 = -1 110 = -2 111 = -3 111 = -0 111 = -1 Issues: balance, number of zeros, ease of operations Which one is best? Why?

TWOS COMPLEMENT NUMBERS

To express negative numbers, the scheme of the binary positional number system is changed slightly - with a large impact. Using n bits, the nth bit is simply changed from positive to negative so that it now represents -(2n-1) . The place values for n=8 bits SIGNED are: -128 64 32 16 8 4 2 1 --- -- -- -- - - - - -27 26 25 24 23 22 21 20 The high bit becomes the "sign bit" only because, if set, it indicates the number is negative. The high bit actually represents -(2n-1). Interestingly, n '1' bits (111..111) is ALWAYS -1 decimal for any n. Why?
COMPARE THESE TWO REPRSENTATIONS -128 64 32 16 8 4 2 1 1 0 0 0 1 0 1 0 = -128 + 8 + 2 = -11810 This number can be represented in hex as 8A16 and in octal as 2128. 128 64 32 16 8 4 2 1 1 0 0 0 1 0 1 0 = 128 + 8 + 2 = 13810 This number can be represented in hex as 8A16 and in octal as 2128. WAIT! - both numbers have the same representation!. How do we know if an 100010102 is -118 or 138? Which is it? How do we know if an 8A16 is -118 or 138? Which is it?
ADDITION EXAMPLE: When we add 0000 0001 0111 1111 --------- We get 1000 0000 If these are 8 bit UNSIGNED numbers that means 1 + 127 = 128. OK. If these are 8 bit SIGNED numbers that means 1 + 127 = -128! NOT OK. The machine can NOT differentiate. It is up to you, the programmer, to understand and interpret correctly. We will look at the Carry (unsigned) and Overflow (signed) Flags that will help us detect these situations.

NEGATING NUMBERS

Numbers using the two's complement scheme can be negated by "taking the two's complement" of the number. The process is two steps: 1. flip the bits (ones complement) 2. add one 0000 1111 = 15 = 8 + 4 + 2 + 1 Negate: 1. flip the bits: 1111 0000 2. add 1 : 1111 0001 1111 0001 = -128 + 64 + 32 + 16 + 1 = -15
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 ----- --- -- -- -- - - - - 215 .. 27 26 25 24 23 22 21 20 1 .. 0 0 0 0 0 0 1 0 = -32768 + 2 = -32766 16 bit signed numbers (2s complement): 0000 0000 0000 0000 = 0 0000 0000 0000 0001 = +1 0000 0000 0000 0010 = +2 0000 0000 0000 0110 = +3 ... 0111 1111 1111 1110 = +32,766 1111 1111 1111 1111 = +32,767 1000 0000 0000 0000 = -32,768 1000 0000 0000 0001 = -32,767 1000 0000 0000 0010 = -32,766 ... 1111 1111 1111 1101 = -3 1111 1111 1111 1110 = -2 1111 1111 1111 1111 = -1 n = 32 ====== If n = 32, the nth bit (2(n-1) ) becomes -2,147,483,648 32 bit signed numbers (2s complement): 0000 0000 0000 0000 0000 0000 0000 0000 = 0 0000 0000 0000 0000 0000 0000 0000 0001 = +1 0000 0000 0000 0000 0000 0000 0000 0010 = +2 0000 0000 0000 0000 0000 0000 0000 0110 = +3 ... 0111 1111 1111 1111 1111 1111 1111 1110 = +2,147,483,646 0111 1111 1111 1111 1111 1111 1111 1111 = +2,147,483,647 1000 0000 0000 0000 0000 0000 0000 0000 = -2,147,483,648 1000 0000 0000 0000 0000 0000 0000 0001 = -2,147,483,647 1000 0000 0000 0000 0000 0000 0000 0010 = -2,147,483,646 ... 1111 1111 1111 1111 1111 1111 1111 1101 = -3 1111 1111 1111 1111 1111 1111 1111 1110 = -2 1111 1111 1111 1111 1111 1111 1111 1111 = -1