170 likes | 268 Views
1. Bits, Data types, and Operations: Chapter 2. COMP 2610. Dr. James Money COMP 2610 . Review of Unsigned Integers. We can write an unsigned integer M=(a k a k-1 … a 2 a 1 a 0 ) 10 a binary number of the form M=(b n b n-1 … b 2 b 1 b 0 ) 2 using simple arithmetic.
E N D
1 Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610
Review of Unsigned Integers • We can write an unsigned integer M=(ak ak-1 … a2 a1 a0)10 a binary number of the form M=(bn bn-1 … b2 b1 b0)2 using simple arithmetic
Review of Unsigned Integers • To convert from binary to decimal, we compute the appropriate bit times the correct power of 2 and add: bnx2n + bn-1x2n-1 + … + b2x22 + b1x21 + b0x20 • For example, (0101)2 = 0x23 + 1x22 + 0x21 + 1x20=4+1=(5)10
Review of Unsigned Integers • To convert from decimal number M to binary: • Set x=M, k=0 • Compute x÷2 = y R bk • Set x=y, k=k+1 • Go to 2 if x≠0 and repeat • The bits are the remainders listed right to left …b3b2b1b0
Review of Unsigned Integers • For example, (11)10: 11 ÷2 = 5 R1 5 ÷ 2 = 2 R1 2 ÷2 = 1 R0 1 ÷2 = 0 R1 Thus, (11)10 = (1011)2
Review of Unsigned Integers • Addition: add the same way as you would for long addition for decimals, except that (1+1)2 = (10)2 and you put down 0 and carry the one. • Example: 0101 5 + 0111+ 7 1100 12
Signed Integers • We need to be able to work with signed integers as well, if for not other reason than to perform subtraction • We can divide the bit codes up into two ranges, one for positive values and one for negative values • For example, 1 to 15, and -1 to -15, plus 0
Signed Integers • How do we do this? • First way is signed magnitude – this uses the first bit as a sign. 0-positive, 1 – negative • The remaining bits are interpreted as the magnitude of the value • For example, -4 for 5 bits is (10100)2 and 4 is (00100)2
Signed Integers • The second approach flips all the bits. Thus for (4)10 = (00100)2 we have that -4 is represented by (11011)2 This is called ones complement.
Signed Integers • The final approach is to use twos complement for the representation. • That is, we compute the ones complement and then add 1. • For example, we have (-4)10 is (11100)2
Signed Integers • Now, we can use any, but only two’s complements works with arithmetic unchanged from unsigned integers. • In addition, in two’s complement has only one representation for zero.
Signed Integers • Consider 4+-2=2 in each representation and using normal arithmetic • Signed Magnitude: 0100 + 1010 = 1110 = (-6)10 • Ones Complement: 0100 + 1101 = 0001 = (1)10 • Twos Complement: 0100 + 1110 = 0010 = (2)10
Signed Integers • Thus, for simplicity of hardware we use the two’s complement representation • How do we compute two’s complement? • Write the magnitude of the number in binary • Compute the one’s complement, that is flip the bits • Add one to the result
Signed Integers • For example, for -9 in 6 bits we have: (9)10 = (001001)2 One’s complement: (110110)2 Adding one: (-9)10 = (110111)2
Signed Integers • How do we reverse two’s complement? • We apply it again! • That is two’s complement(two’s complement(N)) = N
Signed Integers • If the following 6 bits number is in two’s complement form, what is the number? (101001)2 Compute one’s complement: (010110)2 Add one: (010111)2 = (23)10 • Thus, the number is -23