Integer Representation

- Bit-- Binary Digit
- 1 byte = 8 bits
- 1 word = N bytes, take N to be 2 (e.g., 16 bit machine)
- Integer takes up 2 bytes; can be signed or unsigned.

- Can represent whole numbers from 0 to 65,535
- (0 to 2
^{16}- 1). - In binary, this is from
- 0
_{2 }to 1111111111111111^{2 } - Internally, binary representation of decimal value as 16 bits.

- Need to reserve one bit for the sign.
- Three ways:
- Sign-Magnitude
- 1's Complement
- 2's Complement

Sign-Magnitude

- Uses most significant bit of the word to represent the sign.
- 0 - Positive
- 1 - Negative.
- Rest of the number is encoded in magnitude part

37 = 00000000 00100101 -37 = 10000000 00100101

6712 = 00011010 00111000 -6712 = 10011010 00111000

- Can represent numbers from -32,767 to 32,767.
- -2
^{15}+1 .. 2^{15}-1 - But, two representations for zero:

0 = 00000000 00000000 -0 = 10000000 00000000

1's Complement

- Negative number is stored as bit-wise complement of corresponding positive number.
- Leftmost bit of positive number is 0. That of negative number is 1.

196 = 00000000 11000100 -196 = 11111111 00111011

- Can represent numbers from -32,767 to 32,767.
- -2
^{15}+1 .. 2^{15}-1 - Arithmetic is easier than sign-magnitude.
- But, still have two representations for zero:

0 = 00000000 00000000 -0 = 11111111 11111111

2's Complement

- Modern Method
- Positive number represented in same way as other two methods
- Negative number obtained by taking 1's Complement of positive number and adding 1.

6713 = 00011000 00011101 1's Comp = 11100111 11100010 2's Comp = 11100111 11100011

- Word integer can represent numbers from -32,768 to 32,767.
- -2
^{15}.. 2^{15}-1 - Byte integer can represent numbers from -128 to 127.
- -2
^{7 }.. 2^{7}-1 - One version of zero:

00000000 00000000

Conversion of Byte Integer to Word

- Sign Extension
- Copy sign bit of the byte into all the bits of the upper byte of the word.

37 = 00100101 -> 00000000 00100101 -37 = 11011011 -> 11111111 11011011

- converts the signed byte in AL to a word in AX

- Remove upper byte of word. Retain only the lower byte.
- Meaningful only if original number can be represented by a byte.

Integer Arithmetic (1's Comp and 2's Comp)

- Addition: Simply add the two binary representations.
- Subtraction: Find negative of one number, add to the second.

- Add binary representations of the two numbers.
- If there is a carry, add it back in on the right side.

51 00110011 + (-37) + 11011010 ---------- 1 00001101 + 1 ---------- 14 00001110

- Add binary representations of the two numbers.
- Disregard the carry.

51 00110011 + (-37) + 11011011 ---------- 14 1 00001110

Overflow

- If two numbers have different signs, their sum will never overflow.
- If they have the same sign, they might overflow.
- Overflow has occurred if sign of result is different than sign of addends.