Inside the computer ALL arithmetic is done in binary. Even simple things like adding 1. That means ALL numbers must be stored in binary. (There is a method for storing "decimal" numbers, Binary Coded Decimal, but that is not used very often.) If you type in a "normal" number in decimal (base 10), the computer must convert it to binary. There are two methods that people can use to convert decimal numbers to binary - the Subtraction Method or Division Method.
Converting 99 dec to binary means finding powers of 2 that add up to 99. That sounds like a lot of guessing around, but computers don't solve problems by guessing - they follow an algorithm that tells exactly what to do, step by step. Here is an example that illustrates the subtraction algorithm for decimal to binary conversion:
We need to solve the problem: 99 = ____ + ____ + ____ + ____ ... using powers of 2.
Start with 1.
Multiply by 2, over and over again, until
you have a number greater than 99:
1 x 2 ==>
2 x 2 ==> 4 x 2 ==> 8 x 2 ==> 16 x 2 ==> 32 x 2 ==> 64 x
2 ==> 128
Now this power of 2 is too large, so go back one step to
64.
We will need 64 in our sum.
subtract 64 from
99 ==> 35
Now move down to 32, and check whether this is smaller
than 35. It is! So we need it.
subtract 32 from 35 ==>
3.
Now move down to 16 as our power of 2. It's too big,
so we don't need it.
Try 8 , too big, we don't need it.
Try 4, too
big, we don't need it.
Try 2. Yes, it is smaller than 3, so we
need it.
subtract 2 from 3 ==> 1
Move down to 1. Yes we need it.
subtract
1 from 1 ==> 0 **** FINISHED *****
So 99 dec = 64 + 32 + 2 + 1 = 01100011 bin
That is all thoroughly explained, but you will do it more briefly. Convert 47 dec to binary like this:
Start with 32, the biggest power of 2 below 45.
43 >
32 ==> 43 - 32 = 11 output
1
11 < 16 ==> don't use 16 output
0
11 > 8 ==> 11 - 8 = 3 output
1
3 < 4 ==> don't use 3 output
0
3 > 2 ==> 3 - 2 = 1 output
1
1 = 1 ===> 1 - 1 = 0 output
1
Stop.
43
= 101011
A more straightforward algorithm involves repeated division
by 2 (not surprising, considering we are using powers of 2.) This
method is easier to use on big numbers than the subtraction method.
Here
are two examples:
Converting 77 to binary: 77 / 2 = 38 remainder 1 Now read the remainders backward (up): 77 dec = 1001101 bin That means 64 + 8 + 4 + 1? Right!! |
Converting 999 to binary. 999 / 2 = 499 rem 1 999 dec = 1111100111 bin That means 1+2+4+32+64+128+256+512 |
This clever algorithm is good for people to implement. It is also quite easy to program. But keep in mind that the CPU does this much differently (more about that later). Fortunately, you are not a CPU so you won't need to learn its algorithms (unless you become a machine language programmer).
You may choose whether you prefer the division algorithm or the subtraction algorithm, but you must do all the following practice problems WITHOUT using a computer or a calculator. All these problems assume an unsigned binary system (the MSB is not negative).
32 dec = _____________ bin 64 dec = ____________ bin 96 dec = ______________ bin
127 dec = ______________ bin 11001010 bin = _______dec 11001010 = _________ hex
254 dec = ______________ bin 111111 bin = ________dec 0000 0011 = _________ hex
99 hex = ______________ bin 99 hex = ______ dec 99 dec = _______ hex
What is the biggest (unsigned) decimal number that can be stored in 10 binary bits ?
Convert the following signed bytes to decimal
(the MSB is worth -128):
01010101 bin = ____ dec 10101010
bin = _____ dec 11111111 bin
= ______ dec
Write a Java program with a CLI (command line interface) that inputs a decimal integer, performs the Subtraction Method, and displays the binary representation. Assume the number is positive.
Write a Java program to convert from decimal to binary using the Division and Remainder method.