Hold up one hand and give each finger a value: thumb 1, index finger 2, middle finger 4, ring finger 8, little finger 16. A raised finger counts its value, a folded finger counts nothing. Raise the thumb and the middle finger and you are showing 5. Raise all five and you are showing 1+2+4+8+16=311 + 2 + 4 + 8 + 16 = 31. Every whole number from 0 to 31 has exactly one finger pattern, so one hand counts to 31 instead of 5, and two hands reach 210−1=10232^{10} - 1 = 1023. That is the whole idea of binary numbers, and it is the same idea that makes the red, green and blue channels of a screen pixel run from 0 to exactly 255.

So here is how binary works, stated plainly. Binary is base 2. It uses only two digits, 0 and 1, called bits, and each position is worth twice the position to its right: 1, 2, 4, 8, 16, 32 and so on. The place values are the powers of 2, just as the place values of ordinary decimal notation are the powers of 10. Everything else in this article, from converting decimal to binary to two’s complement, hexadecimal and the binary code for letters, follows from that one rule.

What are binary numbers, and how does positional notation work?

The decimal numeral 3507 is shorthand for a sum:

3507=3⋅103+5⋅102+0⋅101+7⋅100.3507 = 3 \cdot 10^3 + 5 \cdot 10^2 + 0 \cdot 10^1 + 7 \cdot 10^0.

Each digit is multiplied by a power of the base, and the position of the digit tells you which power. Nothing in this scheme requires the base to be ten; as our article on why we use base 10 argues, ten is an accident of anatomy. Binary is the idea stripped down to the smallest base that still works.

In base 2 there are two digits, bi∈{0,1}b_i \in \{0, 1\}, and a numeral bn−1bn−2…b1b0b_{n-1} b_{n-2} \ldots b_1 b_0 stands for

N=∑i=0n−1bi 2i=bn−12n−1+⋯+b2⋅4+b1⋅2+b0.N = \sum_{i=0}^{n-1} b_i \, 2^i = b_{n-1} 2^{n-1} + \cdots + b_2 \cdot 4 + b_1 \cdot 2 + b_0.

The rightmost bit b0b_0 is the least significant bit; the leftmost is the most significant. When bases are mixed we write the base as a subscript: 10112=8+0+2+1=11101011_2 = 8 + 0 + 2 + 1 = 11_{10}.

Binary numerals are long. A number N≥1N \geq 1 needs ⌊log⁡2N⌋+1\lfloor \log_2 N \rfloor + 1 bits, so 156 needs 8 bits and one million needs 20, because 219=524,2882^{19} = 524{,}288 falls short and 220=1,048,5762^{20} = 1{,}048{,}576 does not. A tiny alphabet buys long strings: a good bargain for a machine, a poor one for a human reader.

Why does a byte stop at 255?

Computers group bits into bytes of eight. With eight positions the place values run 27,26,…,202^7, 2^6, \ldots, 2^0, that is 128, 64, 32, 16, 8, 4, 2, 1. The figure shows the byte 10110110210110110_2: the blue boxes are the bits that are switched on, and the number is simply the sum of the place values above them.

The byte 10110110 in binary equals 182 in decimal2⁷2⁶2⁵2⁴2³2²2¹2⁰128643216842110110110onoffononoffononoff128 + 32 + 16 + 4 + 2 = 18210110110 in base 2 is 182 in base 10

Figure 1 — The byte 10110110: the on bits sit under 128, 32, 16, 4 and 2, and their place values add up to 182.

The largest byte switches every bit on: 111111112=128+64+32+16+8+4+2+1=25511111111_2 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. There is a slicker way to see that number. Add 1 to 11111111211111111_2 and every bit rolls over, exactly as 999 rolls over to 1000 in decimal, giving 1000000002=28=256100000000_2 = 2^8 = 256. Hence

∑i=072i=28−1=255,\sum_{i=0}^{7} 2^i = 2^8 - 1 = 255,

and more generally ∑i=0n−12i=2n−1\sum_{i=0}^{n-1} 2^i = 2^n - 1. A byte holds 28=2562^8 = 256 patterns, read as 0 through 255. That is why a colour channel stops at 255, why one byte per channel gives 2563=224=16,777,216256^3 = 2^{24} = 16{,}777{,}216 colours, and why the five-finger count tops out at 25−1=312^5 - 1 = 31.

How do you convert binary to decimal?

Multiply each bit by its place value and add, writing the place values above the bits as in Figure 1.

Example 1. Convert 1011012101101_2. From the right the place values are 1, 2, 4, 8, 16, 32, and the 1 bits sit under 32, 8, 4 and 1:

1011012=32+8+4+1=45.101101_2 = 32 + 8 + 4 + 1 = 45.

Example 2. Convert 11001101211001101_2. The 1 bits sit under 128, 64, 8, 4 and 1:

110011012=128+64+8+4+1=205.11001101_2 = 128 + 64 + 8 + 4 + 1 = 205.

A faster method needs no table of powers. Read the bits from left to right and, at each new bit, double the running total and add the bit (Horner’s rule for ∑bixi\sum b_i x^i at x=2x = 2). For 11001101211001101_2:

1→3→6→12→25→51→102→205.1 \to 3 \to 6 \to 12 \to 25 \to 51 \to 102 \to 205.

It works because appending a bit on the right shifts every existing bit up one place, doubling its value.

How do you convert decimal to binary?

There are two standard methods, and it is worth knowing both.

Repeated division by 2. Divide by 2, record the remainder, and continue with the quotient until it reaches 0. Here is 156:

Step Division Quotient Remainder
1 156÷2156 \div 2 78 0
2 78÷278 \div 2 39 0
3 39÷239 \div 2 19 1
4 19÷219 \div 2 9 1
5 9÷29 \div 2 4 1
6 4÷24 \div 2 2 0
7 2÷22 \div 2 1 0
8 1÷21 \div 2 0 1

Reading the remainder column from the bottom up gives 156=100111002156 = 10011100_2. Check: 128+16+8+4=156128 + 16 + 8 + 4 = 156.

Why bottom-up? The first remainder is 156 mod 2156 \bmod 2, the parity of the number, and parity is decided by the ones place alone, since every other place value is even. So the first remainder is b0b_0, the rightmost bit. Dividing by 2 and discarding the remainder shifts the remaining bits one place right, so the next remainder is b1b_1, and so on: the bits come out least significant first. Since each division step has a unique quotient and remainder, this also proves that every positive integer has exactly one binary representation.

Subtract the largest power of 2. Write a 1 under the largest power of 2 that fits, subtract, and repeat. For 156: 128 fits, leaving 28; 64 and 32 do not; 16 fits, leaving 12; 8 fits, leaving 4; 4 fits, leaving 0. The positions 128, 16, 8, 4 are on, so again 156=100111002156 = 10011100_2.

How does binary addition work?

Binary addition is column addition with a very short table:

0+0=0,0+1=1,1+1=102,1+1+1=112.0 + 0 = 0, \quad 0 + 1 = 1, \quad 1 + 1 = 10_2, \quad 1 + 1 + 1 = 11_2.

When a column sums to 2 you write 0 and carry 1; when it sums to 3 (two bits plus an incoming carry) you write 1 and carry 1. Here is 91+5491 + 54:

carries     111111
            01011011    (91)
          + 00110110    (54)
          ----------
            10010001   (145)

Working from the right: the first column gives 1+0=11 + 0 = 1; the second, 1+1=1021 + 1 = 10_2, writes 0 and carries 1; the third and fourth each total 2 with the carry, so write 0 and carry 1; the fifth is 1+1+1=1121 + 1 + 1 = 11_2, write 1 carry 1; the sixth and seventh again write 0 carry 1; and the last is 0+0+1=10 + 0 + 1 = 1. The result is 100100012=128+16+1=14510010001_2 = 128 + 16 + 1 = 145. In hardware the single-column rule is a pair of logic gates, an exclusive-or for the sum and an AND for the carry; how such adders are wired is covered in our article on Boolean algebra.

Shifts. Appending a 0 on the right multiplies by 2, just as appending a 0 in decimal multiplies by 10. Shifting left by kk places multiplies by 2k2^k: 11012=131101_2 = 13, 110102=2611010_2 = 26, 11010002=1041101000_2 = 104. Shifting right halves and discards the remainder, so 13 becomes 1102=6110_2 = 6.

Multiplication as shift-and-add. Each bit of the multiplier is 0 or 1, so binary long multiplication needs no times table: for each 1 bit, add a shifted copy of the multiplicand. For 13×11=11012×1011213 \times 11 = 1101_2 \times 1011_2 the multiplier has 1 bits in positions 0, 1 and 3, so

11012×10112=11012+110102+11010002=13+26+104=143=100011112.1101_2 \times 1011_2 = 1101_2 + 11010_2 + 1101000_2 = 13 + 26 + 104 = 143 = 10001111_2.

This is the ancient Egyptian doubling method, and at bottom it is what a hardware multiplier does.

How do computers store negative numbers? Two’s complement

The obvious idea is sign-magnitude: the leftmost bit is the sign and the other seven bits the size. It has two problems. There are two zeros, 00000000200000000_2 and 10000000210000000_2. Worse, ordinary addition fails: adding 00000101200000101_2 (plus 5) and 10000101210000101_2 (minus 5) gives 10001010210001010_2, which reads as minus 10, not 0.

Almost every modern processor uses two’s complement instead. In an nn-bit two’s complement number the top bit keeps its position but has a negative weight:

N=−bn−12n−1+∑i=0n−2bi 2i.N = -b_{n-1} 2^{n-1} + \sum_{i=0}^{n-2} b_i \, 2^i.

For 8 bits the weights are −128,64,32,16,8,4,2,1-128, 64, 32, 16, 8, 4, 2, 1. The smallest value is 100000002=−12810000000_2 = -128 and the largest is 011111112=12701111111_2 = 127, so the range is −128-128 to 127127: still 256 patterns, now with exactly one zero.

To negate a number, invert every bit and add 1. For 5:

00000101→invert11111010→+111111011.00000101 \xrightarrow{\text{invert}} 11111010 \xrightarrow{+1} 11111011.

Check with the weights: −128+64+32+16+8+2+1=−128+123=−5-128 + 64 + 32 + 16 + 8 + 2 + 1 = -128 + 123 = -5. Read as an unsigned byte, 11111011211111011_2 is 251=256−5251 = 256 - 5. That is no coincidence: an nn-bit register computes modulo 2n2^n, and two’s complement is modular arithmetic with the residues 128 to 255 relabelled as negative.

The payoff is that subtraction becomes addition. Adding plus 5 and minus 5:

00000101+11111011=1 00000000,00000101 + 11111011 = 1\,00000000,

and the ninth bit falls off the end of the 8-bit register, leaving exactly 0. A less symmetrical example, 20−720 - 7: since 7=0000011127 = 00000111_2, minus 7 is 111110002+1=11111001211111000_2 + 1 = 11111001_2 (which is 249=256−7249 = 256 - 7). Then

00010100+11111001=1 00001101  →  000011012=13.00010100 + 11111001 = 1\,00001101 \;\to\; 00001101_2 = 13.

One adder plus a row of inverters handles addition and subtraction, signed and unsigned alike. That economy is why two’s complement won.

The price is overflow. The same wrap-around means results outside the range wrap silently:

127+1=011111112+000000012=100000002=−128.127 + 1 = 01111111_2 + 00000001_2 = 10000000_2 = -128.

Two positives have produced a negative. Hardware flags this (two operands of the same sign giving a result of the opposite sign), but many languages ignore the flag. And negating −128-128 gives 011111112+1=10000000201111111_2 + 1 = 10000000_2, which is −128-128 again, because plus 128 does not fit.

Why do programmers use hexadecimal?

Long strings of bits are hard to read. Since 16=2416 = 2^4, one hexadecimal digit stands for exactly four bits, so conversion is just grouping. The hex digits are 0 to 9, then A to F for 10 to 15. For the byte in Figure 1:

101101102=1011⏟B 0110⏟6=B616,11⋅16+6=182.10110110_2 = \underbrace{1011}_{\text{B}}\,\underbrace{0110}_{6} = \text{B6}_{16}, \qquad 11 \cdot 16 + 6 = 182.

Likewise 156=1001 11002=9C16156 = 1001\,1100_2 = \text{9C}_{16}. A byte is always exactly two hex digits.

Web colours are the most familiar example. The colour code #0071e3 is three bytes, one each for red, green and blue:

Channel Hex Binary Decimal
Red 00 00000000 0
Green 71 01110001 7⋅16+1=1137 \cdot 16 + 1 = 113
Blue E3 11100011 14⋅16+3=22714 \cdot 16 + 3 = 227

So #0071e3 is red 0, green 113, blue 227: the blue of the on bits in Figure 1.

Memory addresses are the other everyday use. A 32-bit address is eight hex digits, and 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 byte addresses is the old 4 GiB limit of 32-bit systems. The largest 32-bit signed integer, 231−1=2,147,483,6472^{31} - 1 = 2{,}147{,}483{,}647, is the tidier 7FFFFFFF16\text{7FFFFFFF}_{16}.

Octal, base 8, groups bits in threes: 10 110 1102=266810\,110\,110_2 = 266_8, and 2⋅64+6⋅8+6=1822 \cdot 64 + 6 \cdot 8 + 6 = 182. It survives in Unix file permissions, where three bits for read, write and execute make one octal digit.

How do you write letters in binary?

A computer stores text by agreeing on a number for every character. The classic binary code is ASCII, a 7-bit code from the 1960s that assigns 0 to 127 to English letters, digits, punctuation and control characters. Capital A is 65, so

A=65=64+1=010000012,\text{A} = 65 = 64 + 1 = 01000001_2,

and the word “Hi” is the two bytes 01001000201001000_2 (72) and 01101001201101001_2 (105). The layout has deliberate structure: lowercase a is 97=01100001297 = 01100001_2, which differs from capital A in a single bit, the one worth 32. Converting case is flipping one bit.

Seven bits give only 27=1282^7 = 128 characters. Unicode assigns a code point to well over a hundred thousand characters, and UTF-8 turns code points into a variable number of bytes:

Code point range Bytes Bit pattern
U+0000 to U+007F 1 0xxxxxxx
U+0080 to U+07FF 2 110xxxxx 10xxxxxx
U+0800 to U+FFFF 3 1110xxxx 10xxxxxx 10xxxxxx
U+10000 to U+10FFFF 4 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

The x positions are filled with the bits of the code point. Plain ASCII text is therefore already valid UTF-8, byte for byte.

Worked example: é. The letter e with an acute accent is U+00E9, which is 233=111010012233 = 11101001_2. It falls in the two-byte range, which has 11 payload bits, so pad it to 00011 10100100011\,101001 and slot the pieces in:

110 00011=C316,10 101001=A916.110\,00011 = \text{C3}_{16}, \qquad 10\,101001 = \text{A9}_{16}.

So é is stored as C3 A9. The euro sign, code point U+20AC, needs three bytes: its 16 bits split as 0010 000010 1011000010\,000010\,101100, giving 1110 0010=E21110\,0010 = \text{E2}, 10 000010=8210\,000010 = \text{82} and 10 101100=AC10\,101100 = \text{AC}. A byte starting with 10 is always a continuation byte, so a program dropped mid-text finds the next character at once. Short codes for common characters is the instinct behind Huffman coding, though UTF-8 is fixed rather than tuned to a text.

Why can’t binary store 0.1 exactly?

Bits after a binary point are worth 12,14,18,…\tfrac12, \tfrac14, \tfrac18, \ldots, so 0.1012=12+18=0.6250.101_2 = \tfrac12 + \tfrac18 = 0.625. To convert a fraction, repeatedly double it and peel off the integer part. For 0.1:

0.1→0.2→0.4→0.8→1.6→1.2→0.4→0.8→1.6→1.2→⋯0.1 \to 0.2 \to 0.4 \to 0.8 \to 1.6 \to 1.2 \to 0.4 \to 0.8 \to 1.6 \to 1.2 \to \cdots

The integer parts give the bits 0,0,0,1,1,0,0,1,1,…0, 0, 0, 1, 1, 0, 0, 1, 1, \ldots, and once 0.20.2 reappears the cycle repeats forever:

0.1=0.00011‾2=0.000110011001100110011…20.1 = 0.0\overline{0011}_2 = 0.000110011001100110011\ldots_2

This is the rule proved in why we use base 10: a fraction in lowest terms terminates in base bb exactly when every prime factor of its denominator divides bb. The denominator 10 contains the prime 5, which does not divide 2.

A computer must cut the expansion off. An IEEE 754 double keeps 53 significant bits, so 0.1 is stored as

0.1000000000000000055511151231257827021181583404541015625,0.1000000000000000055511151231257827021181583404541015625,

slightly more than one tenth. The stored 0.2 is also slightly high and the stored 0.3 slightly low, at 0.2999999999999999888977697537484345957636833190917968750.299999999999999988897769753748434595763683319091796875. Adding 0.1 and 0.2 and rounding gives

0.3000000000000000444089209850062616169452667236328125,0.3000000000000000444089209850062616169452667236328125,

a different double from the one nearest 0.3, which is why 0.1 + 0.2 == 0.3 is false and the sum prints as 0.30000000000000004. Our article on floating-point arithmetic takes the format apart in detail.

Why do computers use binary?

The reason is physical, not mathematical. A circuit represents a bit as a voltage and only has to decide whether it is low or high. Classic TTL logic guarantees outputs of at most 0.4 volts for a 0 and at least 2.4 volts for a 1, while reading any input below 0.8 volts as 0 and above 2.0 volts as 1. That leaves 0.4 volts of noise margin on each side, and every gate restores the signal to a clean level. Ten levels in the same voltage range would leave margins a fraction of the size. Two states also match the true and false of Boolean logic, so the gates that decide also compute. Errors that still slip through are caught by Hamming codes and Reed-Solomon codes, the latter treating each byte as one of the 256 elements of a finite field.

Binary is also the natural unit of information. A bit answers one yes-or-no question, and nn bits distinguish 2n2^n possibilities, so choosing one of NN equally likely items takes log⁡2N\log_2 N bits; twenty questions single out one of 220=1,048,5762^{20} = 1{,}048{,}576, the idea behind binary search. Shannon made this measure the foundation of information theory.

Bits and bytes. A byte is 8 bits. Network speeds are quoted in bits per second, so a 100 megabit per second link moves at most 100/8=12.5100 / 8 = 12.5 megabytes per second. In SI, kilo means 1000, but because 210=10242^{10} = 1024 is so close, computing borrowed kilobyte for 1024 bytes. In December 1998 the International Electrotechnical Commission approved separate binary prefixes, published as an amendment to IEC 60027-2 in early 1999: kibi (Ki) for 2102^{10}, mebi (Mi) for 220=1,048,5762^{20} = 1{,}048{,}576, gibi (Gi) for 230=1,073,741,8242^{30} = 1{,}073{,}741{,}824. A drive sold as 1 TB holds 101210^{12} bytes, which is 1012/230≈931.310^{12} / 2^{30} \approx 931.3 GiB, so a system reporting binary units shows the smaller number.

Who invented binary numbers?

The earliest known European work on binary arithmetic is by the English mathematician Thomas Harriot (1560 to 1621), whose unpublished manuscripts include pages converting between binary and decimal and working additions and multiplications in base 2. The pages are undated and stayed unknown until the twentieth century: Frank Morley noted one in 1922, and John Shirley reproduced two in 1951. They had no influence on what followed.

Binary entered the scholarly record through Gottfried Wilhelm Leibniz, who worked on it from at least 1679, when a manuscript on dyadic progression even sketched a calculating machine using marbles. His published account followed a correspondence with Joachim Bouvet, a French Jesuit in China. In a letter dated 4 November 1701, Bouvet sent Leibniz a woodcut of the Fuxi arrangement of the 64 hexagrams of the I Ching, an ordering attributed to the eleventh-century scholar Shao Yong. Each hexagram is a stack of six lines, solid or broken. If a broken line is read as 0 and a solid line as 1, the arrangement lists the six-bit numbers from 0 to 63 in order. Leibniz received the letter in 1703 and within days sent his paper, Explication de l’Arithmétique Binaire, to the Paris Académie royale des sciences, which printed it in its memoirs for 1703. Its full title claims that binary gives the meaning of the ancient Chinese figures of Fuxi. The match is genuine but formal: the I Ching is a text of divination and cosmology, and nothing suggests its compilers saw the hexagrams as arithmetic. Leibniz had binary first and found the hexagrams afterwards.

The logical half of the story began in 1854, when George Boole published An Investigation of the Laws of Thought, an algebra of true and false. In 1937 Claude Shannon, a master’s student at MIT, wrote the thesis A Symbolic Analysis of Relay and Switching Circuits, showing that Boole’s algebra describes relay circuits and that such circuits can do binary arithmetic. His 1948 paper A Mathematical Theory of Communication calls the units of information “binary digits, or more briefly bits, a word suggested by J. W. Tukey”, the statistician who had used the contraction in a Bell Labs memo the year before.

From Harriot’s notes to Shannon’s relays, the mathematics never changed: a number is a sum of powers of 2, each present or absent. What changed was the discovery that the simplest notation is also the one machines carry out most reliably. Adding 91 and 54, rendering the blue of #0071e3, storing the bytes C3 A9: it is all that one rule, applied very quickly.


Related topics: Boolean Algebra, Why Does 0.1 + 0.2 Not Equal 0.3? Floating-Point Arithmetic Explained, Why Do We Use Base 10?, Modular Arithmetic, Information Theory and Entropy, Hamming Codes, Huffman Coding, Reed-Solomon Codes, Binary Search

Frequently asked

What are binary numbers?

Binary numbers are numbers written in base 2. Only two digits are used, 0 and 1, and each position is worth twice the one to its right: 1, 2, 4, 8, 16 and so on. The binary numeral 1011 means one eight, no four, one two and one one, which is eleven. Every whole number has exactly one binary representation without leading zeros.

How do you convert decimal to binary?

Divide the number by 2 repeatedly and write down each remainder until the quotient reaches 0. Reading the remainders from the last one to the first gives the binary digits. For 156 the remainders are 0, 0, 1, 1, 1, 0, 0, 1, so 156 is 10011100 in binary. Alternatively, subtract the largest power of 2 that fits, mark a 1 in that position, and repeat with what is left.

How do you convert binary to decimal?

Multiply each bit by its place value and add. The place values, from the right, are 1, 2, 4, 8, 16, 32, 64, 128 and so on. For 11001101 the 1 bits sit at 128, 64, 8, 4 and 1, which add up to 205. A faster mental method is to start from the left, and for each new bit double the running total and add the bit.

Why do computers use binary?

Because an electronic component that only has to tell two states apart, such as a low and a high voltage, is cheap, fast and very tolerant of noise. A circuit that had to distinguish ten voltage levels reliably would need far tighter tolerances. Two states also map directly onto the true and false of Boolean logic, so arithmetic and decision making can be built from the same simple gates.

What is two's complement?

Two's complement is the standard way computers store negative integers. To negate a number, invert every bit and add 1. In 8 bits, 5 is 00000101 and minus 5 is 11111011. The leftmost bit carries a weight of minus 128 instead of plus 128, so an 8-bit value ranges from minus 128 to 127, and subtraction can be done with the same circuit as addition.

How do you write letters in binary?

Each character is given a number by an encoding standard, and that number is stored in binary. In ASCII the capital letter A is 65, which is 01000001, and lowercase a is 97, which is 01100001. Modern text uses Unicode with the UTF-8 encoding, which keeps ASCII characters as one byte and uses two to four bytes for everything else; the letter e with an acute accent, for example, becomes the two bytes C3 A9 in hexadecimal.