How binary numbers work
Binary is a place-value system like decimal, but each place is worth twice the one to its right instead of ten times. The only digits are 0 and 1, called bits.
Value = dn × 2n + … + d2 × 4 + d1 × 2 + d0 × 1
Each d is one bit, and its subscript is its position counted from 0 at the right-hand end. So 1011 0110 is 128 + 32 + 16 + 4 + 2 = 182. Octal (base 8) and hexadecimal (base 16) follow the same rule with powers of 8 and 16, and exist mainly as shorthand for binary: one octal digit stands for exactly three bits and one hex digit for exactly four. Splitting 1011 0110 into fours gives B and 6, so it is B6 in hex. Splitting it into threes from the right gives 10, 110, 110, which is 266 in octal.
Decimal to binary: divide by 2 repeatedly and read the remainders from last to first
Worked examples
Converting 156 to binary
- 156 ÷ 2 = 78 remainder 0
- 78 ÷ 2 = 39 remainder 0
- 39 ÷ 2 = 19 remainder 1
- 19 ÷ 2 = 9 remainder 1
- 9 ÷ 2 = 4 remainder 1
- 4 ÷ 2 = 2 remainder 0
- 2 ÷ 2 = 1 remainder 0
- 1 ÷ 2 = 0 remainder 1
Reading the remainders upwards gives 1001 1100. Check: 128 + 16 + 8 + 4 = 156. In hex that is 9C.
Adding 1011 and 110
Binary addition has four rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1). Working from the right: 1 + 0 = 1. Then 1 + 1 = 10, write 0 and carry 1. Then 0 + 1 + 1 = 10, write 0 and carry 1. Then 1 + 0 + 1 = 10, write 0 and carry 1. The final carry goes in front, giving 10001. In decimal that is 11 + 6 = 17. With the same two numbers, 1011 − 110 = 101 (5), 1011 × 110 = 1000010 (66), and 1011 ÷ 110 = 1 remainder 101.
Bitwise operations
Bitwise operations treat a number as a row of separate switches and compare two numbers column by column, with no carrying.
| Operation | Rule for each column | 1100 with 1010 |
|---|---|---|
| AND | 1 only if both bits are 1 | 1000 |
| OR | 1 if either bit is 1 | 1110 |
| XOR | 1 if the bits differ | 0110 |
NOT flips every bit, so it only has a definite answer once the width is fixed: at 8 bits, NOT 0000 1111 is 1111 0000. A left shift moves every bit towards the high end and fills with zeros, which doubles the number for each place. 10110 (22) shifted left by 2 is 1011000 (88). A right shift drops bits off the low end, halving and rounding down: 22 shifted right by 2 is 101 (5).
The commonest practical use is masking. AND with a pattern of 1s keeps just those bits: 182 AND 15 keeps the lowest four bits of 1011 0110 and returns 0110, which is 6. Network subnet masks, file permissions and hardware flags all work this way.
Two’s complement
Computers store negative whole numbers in two’s complement, because it lets the same adding circuit handle both signs.
Pattern for −x at w bits = 2w − x, which is the same as inverting the bits of x and adding 1
Here x is the size of the number without its sign and w is the bit width. To store −42 in 8 bits: 42 is 0010 1010. Inverting gives 1101 0101, and adding 1 gives 1101 0110. As an unsigned number that pattern is 214, and 256 − 42 = 214. The leftmost bit acts as the sign: 1 means negative.
The width sets the range. With w bits the values run from −2w−1 to 2w−1 − 1:
- 8 bits: −128 to 127
- 16 bits: −32,768 to 32,767
- 32 bits: −2,147,483,648 to 2,147,483,647
There is one more negative value than positive, because zero takes one of the non-negative patterns.
Common mistakes
- Reading the remainders in the wrong order. The first remainder is the last bit. Reading 156 downwards gives 0011 1001, which is 57.
- Forgetting the bit width. 1101 0110 is 214 if it is unsigned and −42 if it is 8-bit two’s complement. The bits alone do not say which. Always check the width and whether the value is signed.
- Overflow. In 8-bit two’s complement, 127 + 1 produces 1000 0000, which reads as −128. The addition is correct as bits, but the answer no longer fits the range.
- Grouping from the wrong end. When converting to hex or octal, group bits from the right and pad the leftmost group with zeros. 110110 is 11 0110, which is 36 in hex, not D8.
- Treating a leading zero as meaningful. 0101 and 101 are the same number, 5. Leading zeros only matter when a fixed width is implied.
This page works with whole numbers of up to 64 bits. Binary fractions and floating-point formats are outside its scope.