Converting Signed Integers

Download Report

Transcript Converting Signed Integers

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?
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
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
1.
Consider the leading bit bn. If it is zero, the integer
is positive or zero. If it is one, the integer is
negative.
2.
If bn=1, then compute the two’s complement of bn1…b1b0. Then
bn-1x2n-1 + … + b2x22 + b1x21 + b0x20
is the magnitude of the value
3.
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:
1.
1100 0011
2.
0011 0110
3.
1111 1110
Decimal to Binary Conversion
1.
Take the magnitude of the decimal number
2.
Convert the magnitude of the number to
binary form
3.
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:
1.
Set x=M, k=0
2.
Compute x÷2 = y R bk
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
…b3b2b1b0
Decimal to Binary Conversion

Convert the following decimal numbers to
their two’s complement forms for 8 bits:
1.
45
2.
-34
3.
-2
4.
0