20 == 16 + 4
16 is 10000, 4 is 00100, so 20 must be 10100.
The above ideas lead to the repeated subtraction method of converting a reprsentation of a positive integer from decimal to binary.
| Algorithm: Convert a positive integer from base 10 to Binary Representation |
|---|
number = positive integer;
while (number > 0 )
{
determine the largest 2B that is less than or equal to number;
put a 1 in the B's place of the binary ;
number = number - 2B ;
}
Put 0s in the binary for the powers of 2 not used;
|
You are (of course) eager to practice this method. Let us convert 23 to binary.
What is the greatest power of two less than 23?