Binary Calculations

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.

Subtraction 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:

That is all thoroughly explained, but you will do it more briefly.  Convert 47 dec to binary like this:

Divide (and Conquer)

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
    38 / 2 = 19   remainder  0
    19 / 2 = 9     remainder  1
    9 / 2  =  4     remainder  1
    4 / 2 = 2       remainder  0
    2 / 2 = 1       remainder  0
    1 / 2 = 0       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
    499 / 2 = 249   rem   1
    249 / 2 = 124   rem   1
    124 / 2 = 62     rem   0
    62 / 2   = 31     rem   0
    31 / 2   = 15     rem   1
    15 / 2   =  7      rem   1
    7 / 2     =  3      rem   1
    3 / 2     =  1      rem   1
    1 / 2     =  0      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).

Practice

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.