Unsigned Binary Integer

BITS

High order bit is leftmost, low order (least significant) bit is rightmost. The location of each bit determines its value:

33222222222211111111110000000000
10987654321098765432109876543210
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|                    |         |
231 place              210       20
EXAMPLE

What is the 32-bit unsigned binary integer representation for the decimal integer 86420?

SOLUTION

1.Succesively divide by 2 until the result is zero. The remainders of these divisions will be the bits used to construct the solution.

86420 ÷ 2 = 43210 rem 0     Low order bit
43210 ÷ 2 = 21605 rem 0
21605 ÷ 2 = 10802 rem 1
10802 ÷ 2 =  5401 rem 0
 5401 ÷ 2 =  2700 rem 1
 2700 ÷ 2 =  1350 rem 0
 1350 ÷ 2 =   675 rem 0
  675 ÷ 2 =   337 rem 1
  337 ÷ 2 =   168 rem 1
  168 ÷ 2 =    84 rem 0
   84 ÷ 2 =    42 rem 0
   42 ÷ 2 =    21 rem 0
   21 ÷ 2 =    10 rem 1
   10 ÷ 2 =     5 rem 0
    5 ÷ 2 =     2 rem 1
    2 ÷ 2 =     1 rem 0
    1 ÷ 2 =     0 rem 1     High order bit
2. Organize the remainder bits into the binary integer:
10101000110010100
3. Pad the bits to the left with zeroes to bring to a total of 32:
000000000000000 10101000110010100
4. Convert to hex:
0000 0000 0000 0001 0101 0001 1001 0100

00015194




Sign-Magnitude Signed Binary Integer

BITS
33222222222211111111110000000000
10987654321098765432109876543210
SBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
where
S = 1 for negative number, 0 for non-negative
The remaining bits in position n represents 2n place.

EXAMPLE

What is the 32-bit sign-magnitude binary integer representation for the decimal integer -47?


SOLUTION

1. S = 1, for negative number.

2. Solve as for an unsigned integer for the remaining 31 bits.

4710 = 1011112
3. Organize the bits, padding with zeroes between the sign and the magnitude:
1 0000000000000000000000000 101111

4. Convert to hex:
1000 0000 0000 0000 0000 0000 0010 1111

8000002F




2's-Complement Signed Binary Integer

BITS

33222222222211111111110000000000
10987654321098765432109876543210
SBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB


where
S = 0 for non-negative numbers

Interpretation:

the remaining 31 bits represent the magnitude of the non-negative number.
S = 1 for negative numbers

Interpretation:

Invert all the bits 2's-complement number.
Add 1.
The resulting 32 bits represent the magnitude of the negative number.

Note: this is the same as subtracting 1 from the 2's-complement number and then inverting the bits. Click here for more background.

EXAMPLE 1

What is the 32-bit 2's-complement signed binary integer representation for the decimal integer -47?


SOLUTION

1. Solve as for an unsigned integer for the magnitude:

4710 = 1011112
2. Pad with zeroes to form a 32-bit number:
00000000000000000000000000 101111
3. If the value was non-negative, the answer is now complete.

4. For a negative number we must next invert all the bits:

11111111111111111111111111010000
5. Add one to the result:
11111111111111111111111111010001
6. Convert to hex:
1111 1111 1111 1111 1111 1111 1101 0001

FFFFFFD1


EXAMPLE 2

What is the value of the 2's-complement number represented by the hexadecimal number FFFFFF99?


SOLUTION

1. Write out the bits:

1111 1111 1111 1111 1111 1111 1001 1001
2. Since the first bit is a 1 this is a negative number. We must continue. Invert all the bits.
0000 0000 0000 0000 0000 0000 0110 0110
3. Add 1.

0000 0000 0000 0000 0000 0000 0110 0111
4. Convert this result to decimal:
11001112 = 10310
5. Since the value is negative, the original binary number was the 2's-complement representation of the decimal number -103.


BINARY ADDITION OF 2'S-COMPLEMENT NUMBERS

Binary addition of a 2's-complement signed integer is very simple. The rules are the same as decimal addition, except that the carry of 1 happens when 1 is added to 1. That is:

0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 0, carry the 1
A carry from the most significant bit position is discarded. The carry out from the most significant bit must be the same as the carry in to that bit (either both 0 or both 1), otherwise an overflow or underflow error has occurred.

For example, using 4-bit 2's-complement signed binary integers:
  0 0 1 1  note: 00112 = 310
+ 0 0 1 1  =  3
---------
  0 1 1 0  =  6; carry in = carry out of sign bit = 0

  1 0 0 1  = -7
+ 0 0 1 1  =  3
---------
  1 1 0 0  = -4; carry in = carry out = 0

  1 0 0 1  = -7
+ 1 0 0 1  = -7
---------
  0 0 1 0  = ERROR; 1 = carry out bit; 0 = carry in to sign bit

  0 1 1 1  =  7
+ 0 1 1 1  =  7
---------
  1 1 1 0  = ERROR, 0 = carry out; 1 = carry in

  1 1 0 1  = -3
+ 1 1 0 1  = -3
---------
  1 0 1 0  = -6; carry out = carry in = 1; carry out bit is discarded.

  1 1 0 1  = -3
+ 0 0 1 1  =  3
---------
  0 0 0 0  = 0; carry out = carry in = 1; carry out bit is discarded.





IEEE 32-Bit Floating Point Format

BITS
33222222222211111111110000000000
10987654321098765432109876543210
SEEEEEEEEFFFFFFFFFFFFFFFFFFFFFFF

= (-1)s2e-1271.f

where
s = S
e = EEEEEEEE, e > 0, e < 255
f = FFFFFFFFFFFFFFFFFFFFFFF

Denormals: e = 0; Specials: (NaN, Inf, -Inf) e = 255


EXAMPLE

What is the IEEE 32-bit floating point representation for the decimal number -11.5?


SOLUTION

1. Convert to binary. To the left of the binary point, represent the magnitude of the number to the left of the decimal point. To the right of the binary point, represent the fraction to the right of the decimal point (note: this may require a loss of accuracy). The first position after the binary point is the 2-1 position (0.5 decimal), then 2-2 (0.25), 2-3 (0.125), etc.

-11.510 = -1011.12
2. Convert to normalized binary scientific notation (i.e. move binary point to the left or right as far necessary until a single one is to left of the binary point, e.g. 1.f):
-1011.12 = -1.0111 x 23
Note: in the special case of 0.0, all 32 bits are 0. This is a denormal since there is no 1 to the left of the binary point.

3. Determine s, e and f:

s = 1 for negative, 0 for positive.

true exponent = 310 = e - 127, thus
e = 3 + 127 = 13010 = 100000102

1.f = 1.0111, thus f = 0111
4. Assemble the 32 bits, padding f to the right with zeroes:
s   e         1.fffffffffffffffffffffff
1   10000010    01110000000000000000000

11000001001110000000000000000000

5. Convert to hex:
1100 0001 0011 1000 0000 0000 0000 0000

C1380000
Note special cases:

s = 0, e=255, f = all zeroes: +Infinity
s = 1, e=255, f = all zeroes: -Infinity
s = 0 or 1, e=255, f = anything but all zeroes: Not A Number