100 likes | 213 Views
1. Bits, Data types, and Operations: Chapter 2. COMP 2610. Dr. James Money COMP 2610. 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
E N D
1 Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610
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 • 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
Binary to Decimal Conversion • We now systematically look at convert from binary to decimal for two’s complement stored integers • We assume the binary number is in the form M=(bn bn-1 … b2 b1 b0)2 where bi is either 0 or 1.
Binary to Decimal Conversion • Consider the leading bit bn. If it is zero, the integer is positive or zero. If it is one, the integer is negative. • If bn=1, then compute the two’s complement of bn-1…b1b0. Then bn-1x2n-1 + … + b2x22 + b1x21 + b0x20 is the magnitude of the value • If the leading bit bn was 1, then add the negative sign.
Binary to Decimal Conversion • Convert the following two’s complement representations to their decimal values: • 1100 0011 • 0011 0110 • 1111 1110
Decimal to Binary Conversion • Take the magnitude of the decimal number • Convert the magnitude of the number to binary form • If the number is negative, compute the two’s complement of the number
Review of Unsigned Integers • To convert from positive 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
Decimal to Binary Conversion • Convert the following decimal numbers to their two’s complement forms for 8 bits: • 45 • -34 • -2 • 0