Math

Formatting decimal as binary

While most of the time you will see numbers in the decimal number system (using the digits 0 through 9), internally within your computer these numbers will be represented in binary. In the binary numeral system each number can only be composed of a sequence of two values: 0 or 1, ON or OFF, TRUE or FALSE, and so on.

Binary numbers are sequences of 1′s and 0′s, moving from right to left, each occurrence of a 1 is equivalent to twice the decimal value of the previous digit’s decimal value. Take for example the binary number 11111111. The first digit has the decimal value of 1, the second has the decimal value of 2, the third has the decimal value of 4, and so on. Adding all these values together equals the final decimal value, which in this case is 255.

Each digit in a binary number is called a bit, in this case all bits are 1. If some of the bits were 0, they would not add to the resulting decimal number, but they would still represent a place within the sequence.

To play around with converting numbers from one base to another, try my Base Converter widget.

Treating decimal numbers as binary numbers is useful when dealing with bitwise programming, where individual bits within a binary number can be switched to their reverse value (either 1 or 0). This allows for extremely efficient division by 2 arithmetic and for a technique of representing binary numbers as an efficient storage mechanism for a bunch of boolean values.