# Binary Numbers

In the binary system, each digit's position signifies a specific power of two. Think of a binary number as a way to indicate which powers of two are needed so that their sum equals the given number.

Number in base-10 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Sum |

114 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 114 = 64 + 32 + 16 + 2 |

12 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 12 = 8 + 4 |

13 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 13 = 8 + 4 + 1 |

Each 1 in the binary representation signifies that the corresponding power of two is part of the sum.

The beauty of the binary system lies in its simplicity and uniqueness. Each number has one and only one binary representation, made up of a sum of distinct powers of two. Now that we understand what each bit represents, converting a binary number to its base-10 equivalent becomes straightforward. All we have to do is sum up the corresponding powers of two for each bit that is set to '1.’

What About the Opposite Task?

We've discussed converting binary numbers to decimal (base-10), but what about doing the reverse? How can we convert a decimal number into its binary representation?

Finding the Rightmost Bit

To start, let's consider how to find the rightmost digit (rightmost bit) in the binary representation of a number. In base-10, if we want to find the last digit of a number, we can simply take the remainder when divided by 10, often denoted as

**% 10**

in programming languages. Similarly, in binary, the rightmost bit of a number can be found by taking the remainder when the number is divided by 2, or **% 2**

. This also means that the rightmost bit is always 1 for odd numbers and 0 for even numbers. For example:

- For 114 its rightmost bit is equal to 0, since 114 % 2 = 0

- For 13 its rightmost bit is equal to 1, since 13 % 2 = 1

Shifting a Binary Number

Once we've found the rightmost bit, the next step is to shift the entire binary number one position to the right. This operation is equivalent to floor-dividing the number by 2 (often denoted as

**/ 2**

in most programming languages or `// 2`

in Python). This operation effectively removes the rightmost bit, making the next bit in line the new rightmost bit.- Shifting 114 ( in binary) to the right yields which is equal to 114 / 2 = 57 in base-10

- Shifting 13 ( in binary) to the right yields which is 13 / 2 = 6 in base-10

The Conversion Algorithm

With these two operations—finding the rightmost bit and shifting the number—we can devise a simple algorithm to convert a base-10 number to binary:

- Initialize an empty string to store the binary representation.

- While the number is greater than 0:
- Find the rightmost bit by taking the remainder when divided by 2.
- Add this bit to the left of your binary string.
- Floor-divide the number by 2 to shift it to the right.

- If the string is empty (which means the original number was 0), set the binary string to '0'.

- Return the binary string as the result.

Using this algorithm, you can convert any base-10 number to its unique binary representation.