120 likes | 300 Views
Number Systems and Circuits for Addition – Negative Integers. Lecture 7 Section 1.5 Mon, Jan 29, 2007. Addition of Fixed-Length Integers. Represent 150 and 106 as 8-bit integers. 150 = 10010110 . 106 = 01101010 . Express the sum as an 8-bit integer. 10010110 + 01101010 = 00000000 .
E N D
Number Systems and Circuits for Addition – Negative Integers Lecture 7 Section 1.5 Mon, Jan 29, 2007
Addition of Fixed-Length Integers • Represent 150 and 106 as 8-bit integers. • 150 = 10010110. • 106 = 01101010. • Express the sum as an 8-bit integer. • 10010110 + 01101010 = 00000000. • Carry-out bit is thrown away. • Conclusions • 150 + 106 = 0. • 150 = –106.
Two’s Complement • For binary numbers of fixed length n, the two’s complement of a is 2n – a • If a > 0, then –a is stored as the two’s complement of a. • The two’s complement of the two’s complement of a is a.
Finding the Two’s Complement • To find the two’s complement of an n-bit binary number: • Reverse each bit, including leading zeros. • Add 1 to the result. • Reversing each bit is equivalent to subtracting from 111…1 = 2n – 1.
It’s a Matter of Interpretation 11111111 : 10000001 10000000 01111111 : 00000001 00000000 Signed -128to-1 Unsigned 0to255 Signed 0to127
It’s a Matter of Interpretation 11111111 : 10000001 10000000 01111111 : 00000001 00000000 Signed -128to-1 Unsigned 0to255 +1 +1 Signed 0to127
It’s a Matter of Interpretation 11111111 : 10000001 10000000 01111111 : 00000001 00000000 Signed -128to-1 Unsigned 0to255 –1 –1 Signed 0to127
Signed Integers • Let P = {0, …, 127} • Let N = {–128, …, –1} • High-order bit • Members of P have high-order bit = 0. • Members of N have high-order bit = 1. • Signed vs. unsigned • If nP, then signed(n) = unsigned(n). • If nN, then signed(n) = unsigned(n) – 256.
Some Special Values • 0 = 00000000 • 127 = 01111111 • –128 = 10000000 • – 1 = 11111111 • What value is 00000001? • What value is 10000001? • What value is 11111110? • What value is 01111110?
2-Byte vs. 4-Byte Integers • Assign a short to an int. • Exactly what bits are copied to what locations? • The sign bit must be extended. short s = -1; int i = s;