Working With Binary Numbers 1 Converting from binary to decimal

advertisement
FORDHAM UNIVERSITY
Fordham College Lincoln Center
Dept. of Computer and Info. Science
CISC 3593
Computer Organization
Spring, 2011
Working With Binary Numbers
This document describes some methods, not given in the textbook, for converting binary integers to decimal and vice versa. It is assumed the binary
integers are either unsigned or in the two’s complement system.
1
Converting from binary to decimal
The algorithm for doing this conversion is first described in terms of unsigned
binary numbers.
Double-and-add algorithm
1. If the binary number is all 0s, the result is 0. Stop. Otherwise,
2. Begin at the leftmost (most significant) 1 bit. Initialize the result to 1.
3. If you have reached the rightmost (least significant) bit, stop. Otherwise,
4. Double the result and add the next bit to the right. Go to step 3.
Example: Given the number 0011 1001. The sequence of results is 1, 3, 7, 14,
28, 57. The value of the number is 57.
Example: Given the number 0100 0100. The sequence of results is 1, 2, 4, 8,
17, 34, 68. The value of the number is 68.
1.1
Negative numbers
The decimal number system we are accustomed to can be termed signed-magnitude
decimal since negative numbers are represented by their magnitude prefixed by
a sign. (Of course, the plus sign for positive numbers is usually omitted.) Signed
binary numbers on the other hand use the two’s complement system. Positive
numbers in this system are the same as in unsigned binary. Negative numbers
are stored as the two’s complement of their magnitude.
Therefore, the first step in converting a two’s complement binary number to
decimal is to examine the leftmost bit, which is the sign bit. If it is 0, then the
number is positive and you can proceed to apply the double-and-add algorithm
directly. If the sign bit is 1, then you need to first take the two’s complement
of the number to obtain its magnitude. Then convert the magnitude to decimal
and prefix it with a minus sign.
The simplest method for taking the two’s complement of a number is as
follows:
1
Two’s complement operation
1. Locate the rightmost (least significant) 1 bit. (If all bits are 0, pretend
there is a 1 bit to the left of the leftmost bit.)
2. Complement all bits to the left of that bit.
3. Leave unchanged that bit and all the 0 bits to its right.
Example: Given the number 1011 1001. The leftmost bit is 1, so this number is
negative. Its magnitude is found by complementing all bits except the rightmost:
0100 0111. Then applying the double-and-add algorithm to this number, the
sequence of results is 1, 2, 4, 8, 17, 35, 71. The original value is therefore −71.
Example: Given the number 1100 0100. This is negative. Forming the two’s
complement, the magnitude is 0011 1100. The double-and-add algorithm gives
1, 3, 7, 15, 30, 60. The original value is therefore −60.
2
Converting from decimal to binary
A rapid method for converting from decimal to binary is the reverse of the
double-and-add method. We first present it for positive numbers.
Divide-and-remainder algorithm
1. Set the working value equal to the given decimal number.
2. If the working value is 0, stop. Otherwise,
3. Divide the working value by 2. Write down the remainder (0 or 1).
4. Replace the working value by the quotient. Go back to step 2.
The sequence of remainders represent the bits of the binary value, from least to
most significant. Therefore it can be convenient to write the sequence of values
from right to left, with the bits underneath.
Example: Given the decimal number 57. Writing the working values from
right to left,
1 3 7 14 28 57
1 1 1
0
0
1
Padding at the left with 0s to make an 8-bit word size, the result is 0011 1001.
Note how the bits underneath the quotients are simply 0 for even values and 1
for odd values.
Example: Given the decimal number 68.
1 2 4 8 17 34 68
1 0 0 0
1
0
0
Padding at the left with a 0 to make an 8-bit word size, the result is 0100 0100.
2
2.1
Negative numbers
If the given decimal number is negative, first convert its magnitude to binary
using the divide-and-remainder algorithm, then form the two’s complement to
negate it in binary.
Example: Given the decimal number −57. Its binary value was worked out
above as 0011 1001. Applying the two’s complement operation, we obtain 1100
0111 as the negative binary value.
Example: Given the decimal number −68. Its binary value was worked out
above as 0100 0100. Applying the two’s complement operation, we obtain 1011
1100 as the negative binary value.
3
Using bc
Obviously the above methods are tiresome to do by hand, even with the help of
a calculator, especially for 16-bit or larger word sizes. Some pocket calculators
will perform number conversions for you. This section describes a utility that
you can use on any Linux system.
Note: you may use a calculator or this utility for homework to check your
work, but not to actually do the exercises on number conversion.
There is a standard Unix utility named bc that can do arbitrary-precision
arithmetic and input or output values in any chosen base. (Its name stands for
“better calculator” since its author felt it was an improvement over the earlier
dc, “desk calculator.”)
The bc program normally accepts arithmetic expressions (with no prompt)
and prints the results. Use Ctrl-D to exit.
You can set the input base using the command ibase=b where b is any
integer greater than 1. Thereafter it will assume that all input numbers are
expressed in base b.
You can set the output base using the command obase=b where b is any
integer greater than 1. Thereafter it will print all results in base b.
There are a few caveats to keep in mind.
1. If you first set ibase to some value other than 10, the value you give for
obase will be read in the new input base. For instance, if you say ibase=2,
then obase=10, the output base will be 2ten since that is what 10 means
in the new input base.
2. Negative numbers are represented in signed-magnitude form. Using obase=2,
the number −4 will be printed as -100. To get the two’s complement of a
number, subtract it from 2n where n is the number of bits in your chosen
word size. The operator ^ represents exponentiation.
3. It is your responsibility to enter valid numbers in the given input base b.
If you use digits ≥ b, your results will be whatever bc decides to do with
them.
3
4. For input of hexadecimal numbers, you need to use uppercase letters for
the digits A through F. (Lowercase letters are reserved for variables.)
Example: Using bc to convert from binary to decimal:
[janeuser@erdos ~]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type ‘warranty’.
ibase=2
00111001
57
01000100
68
^D
Example: Using bc to convert from decimal to binary:
[janeuser@erdos ~]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type ‘warranty’.
obase=2
57
111001
2^8-57
11000111
^D
In the last input line, we calculated −57 as the two’s complement of 57, subtracting the magnitude from 28 . If we had entered -57, it would simply have
printed -111001.
4
Download