Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610 1.

Similar presentations


Presentation on theme: "Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610 1."— Presentation transcript:

1 Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610 1

2 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

3 Review of Unsigned Integers To convert from binary to decimal, we compute the appropriate bit times the correct power of 2 and add: b n x2 n + b n-1 x2 n-1 + … + b 2 x2 2 + b 1 x2 1 + b 0 x2 0 For example, (0101) 2 = 0x2 3 + 1x2 2 + 0x2 1 + 1x2 0 =4+1=(5) 10

4 Review of Unsigned Integers To convert from decimal number M to binary: 1. Set x=M, k=0 2. Compute x÷2 = y R b k 3. Set x=y, k=k+1 4. Go to 2 if x≠0 and repeat 5. The bits are the remainders listed right to left …b 3 b 2 b 1 b 0

5 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

6 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: 01015 +0111+7 1100 12

7 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

8 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

9 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.

10 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

11 Signed Integers RepresentationSigned Magnitude 1’s Complement 2’s Complement 0000000 0001111 …. 0111777 1000-0-7-8 1001-6-7 1010-2-5-6 1011-3-4-5 1100-4-3-4 1101-5-2-3 1110-6-2 1111-7-0

12 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.

13 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

14 Signed Integers Thus, for simplicity of hardware we use the two’s complement representation How do we compute two’s complement? 1. Write the magnitude of the number in binary 2. Compute the one’s complement, that is flip the bits 3. Add one to the result

15 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

16 Signed Integers How do we reverse two’s complement? We apply it again! That is two’s complement(two’s complement(N)) = N

17 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


Download ppt "Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610 1."

Similar presentations


Ads by Google