Binary numbers
Binary numbers are a number system with base 2. In the binary system there are only two digits: 0 and 1.
Each digit in a binary number has a place value, which is a power of 2.
From right to left the place values are \(2^0, 2^1, 2^2, 2^3, \ldots\).
$$ \large 10101_2 = 1\cdot2^4 + 0\cdot2^3 + 1\cdot2^2 + 0\cdot2^1 + 1\cdot2^0 = 16 + 0 + 4 + 0 + 1 = 21_{10} $$
From binary to decimal
To convert a binary number to decimal, add the place values where there is a 1.
$$ \large 11001_2 = 1\cdot2^4 + 1\cdot2^3 + 0\cdot2^2 + 0\cdot2^1 + 1\cdot2^0 = 16 + 8 + 0 + 0 + 1 = 25_{10} $$
From decimal to binary
A quick method is to find the powers of 2 that add up to the number. Put 1 in the places you use and 0 in the others.
Example: 37 in decimal can be written as \(32 + 4 + 1\). This corresponds to \(2^5 + 2^2 + 2^0\).
$$ \large 37_{10} = 100101_2 $$
Here are the numbers from 0–15. We use 4 bits with leading zeros for clarity.
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Binary (4 bits) | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
Bits and bytes
A bit is a single binary digit (0 or 1). A byte is 8 bits. The largest value on 8 bits is \(11111111_2\), which equals 255 in decimal.
$$ \large 11111111_2 = 2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0 = 128+64+32+16+8+4+2+1 = 255_{10} $$
Binary addition
The rules for one column are: \(0+0=0\), \(0+1=1\), \(1+0=1\), \(1+1=10\) (write 0 and carry 1 to the next column).
Example:
$$ \large 1011_2 + 110_2 = 10001_2 $$
Binary subtraction
When subtracting numbers in binary, use the same method as in decimal. If the digit above is too small, borrow from the next column.
Example: \(1010_2 - 11_2\)
$$ \large 1010_2 - 0011_2 = 0111_2 $$
Here we borrow from the third column, so \(0 - 1\) becomes \(10 - 1 = 1\), etc. The result 0111 is 7 in decimal.
Binary multiplication and division
Multiplication follows the same logic as in decimal, but you only multiply by 0 or 1. Division also follows the same rules as in decimal.
Example of multiplication:
$$ \large 101_2 \cdot 11_2 = 1111_2 $$
Explanation: \(101_2 = 5_{10}\), \(11_2 = 3_{10}\). So the result \(1111_2\) equals \(15_{10}\).
Example of division:
$$ \large 1100_2 \div 11_2 = 100_2 $$
Explanation: \(1100_2 = 12_{10}\), \(11_2 = 3_{10}\). The result \(100_2 = 4_{10}\).
Typical patterns
A binary number that consists only of 1’s corresponds in decimal to the sum of a series of powers of 2.
$$ \large 1111_2 = 2^3+2^2+2^1+2^0 = 8+4+2+1 = 15_{10} $$
$$ \large 1000_2 = 2^3 = 8_{10} $$
Summary
Binary numbers use only the digits 0 and 1, and each position carries the weight of a power of 2.
You convert by using the place values \(2^0, 2^1, 2^2, \ldots\), and you can go the other way by breaking the decimal number into powers of 2.