LESSON 03 – CONVERSIONS FROM BASE 10

 

 

LESSON NOTE

FROM BASE 10 TO BASE 2

 

OPTION #1 – SUBTRACTION APPROACH

 

In this approach, we check if our number is larger than a power of two.  We start with 128, then 64, then 32, …  If the number is greater than the number it is checked against, our next digit is 1, otherwise, it is 0. 

STEP #1

if number greater than or equal to 128

   Leftmost digit is 1

   number = number - 128

else

   Leftmost digit is 0

end if

 

STEP #2

if number greater than or equal to 64

   Next digit is 1

   number = number - 64

else

            Next digit is 0

end if

STEP #3

if number greater than or equal to 32

   Next digit is 1

   number = number - 32

else

   Next digit is 0

end if

 

STEP #4

if number greater than or equal to 16

   Next digit is 1

   number = number - 16

else

   Next digit is 0

end if

 

STEP #5

if number greater than or equal to 8

   Next digit is 1

   number = number - 8

else

   Next digit is 0

end if

 

STEP #6

if number greater than or equal to 4

   Next digit is 1

   number = number - 4

else

   Next digit is 0

end if

 

STEP #7

if number greater than or equal to 2

   Next digit is 1

   number = number - 2

else

   Next digit is 0

end if

 

STEP #8

if number is equal to 1

   Rightmost digit is 1

   number = number - 1

else

   Rightmost digit is 0

end if

 

OPTION #2 – REMAINDER APPROACH

 

We simply continuously divide our number by two.  The remainder (0 or 1) is a digit in our binary number.  An example is the best way to understand (and explain) the method.

 

Example – Convert 157 to binary.

 

157 ÷ 2 = 78
78 ÷ 2 = 39
39 ÷ 2 = 19
19 ÷ 2 = 9
9 ÷ 2 = 4
4 ÷ 2 = 2
2 ÷ 2 = 1
1 ÷ 2 = 0

with a remainder of 1
with a remainder of 0
with a remainder of 1
with a remainder of 1
with a remainder of 1
with a remainder of 0
with a remainder of 0
with a remainder of 1

(the bottom is the leftmost number)

 

Therefore 10011101 is our answer.  This approach may not give you 8 digits.  You may be forced to tack on zeros at the beginning.