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 . 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 . 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:
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, , and a numeral stands for
The rightmost bit is the least significant bit; the leftmost is the most significant. When bases are mixed we write the base as a subscript: .
Binary numerals are long. A number needs bits, so 156 needs 8 bits and one million needs 20, because falls short and 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 , that is 128, 64, 32, 16, 8, 4, 2, 1. The figure shows the byte : the blue boxes are the bits that are switched on, and the number is simply the sum of the place values above them.
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: . There is a slicker way to see that number. Add 1 to and every bit rolls over, exactly as 999 rolls over to 1000 in decimal, giving . Hence
and more generally . A byte holds patterns, read as 0 through 255. That is why a colour channel stops at 255, why one byte per channel gives colours, and why the five-finger count tops out at .
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 . From the right the place values are 1, 2, 4, 8, 16, 32, and the 1 bits sit under 32, 8, 4 and 1:
Example 2. Convert . The 1 bits sit under 128, 64, 8, 4 and 1:
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 at ). For :
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 | 78 | 0 | |
| 2 | 39 | 0 | |
| 3 | 19 | 1 | |
| 4 | 9 | 1 | |
| 5 | 4 | 1 | |
| 6 | 2 | 0 | |
| 7 | 1 | 0 | |
| 8 | 0 | 1 |
Reading the remainder column from the bottom up gives . Check: .
Why bottom-up? The first remainder is , 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 , the rightmost bit. Dividing by 2 and discarding the remainder shifts the remaining bits one place right, so the next remainder is , 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 .
How does binary addition work?
Binary addition is column addition with a very short table:
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 :
carries 111111
01011011 (91)
+ 00110110 (54)
----------
10010001 (145)
Working from the right: the first column gives ; the second, , writes 0 and carries 1; the third and fourth each total 2 with the carry, so write 0 and carry 1; the fifth is , write 1 carry 1; the sixth and seventh again write 0 carry 1; and the last is . The result is . 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 places multiplies by : , , . Shifting right halves and discards the remainder, so 13 becomes .
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 the multiplier has 1 bits in positions 0, 1 and 3, so
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, and . Worse, ordinary addition fails: adding (plus 5) and (minus 5) gives , which reads as minus 10, not 0.
Almost every modern processor uses two’s complement instead. In an -bit two’s complement number the top bit keeps its position but has a negative weight:
For 8 bits the weights are . The smallest value is and the largest is , so the range is to : still 256 patterns, now with exactly one zero.
To negate a number, invert every bit and add 1. For 5:
Check with the weights: . Read as an unsigned byte, is . That is no coincidence: an -bit register computes modulo , 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:
and the ninth bit falls off the end of the 8-bit register, leaving exactly 0. A less symmetrical example, : since , minus 7 is (which is ). Then
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:
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 gives , which is again, because plus 128 does not fit.
Why do programmers use hexadecimal?
Long strings of bits are hard to read. Since , 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:
Likewise . 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 | |
| Blue | E3 | 11100011 |
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 byte addresses is the old 4 GiB limit of 32-bit systems. The largest 32-bit signed integer, , is the tidier .
Octal, base 8, groups bits in threes: , and . 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
and the word “Hi” is the two bytes (72) and (105). The layout has deliberate structure: lowercase a is , which differs from capital A in a single bit, the one worth 32. Converting case is flipping one bit.
Seven bits give only 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 . It falls in the two-byte range, which has 11 payload bits, so pad it to and slot the pieces in:
So é is stored as C3 A9. The euro sign, code point U+20AC, needs three bytes: its 16 bits split as , giving , and . 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 , so . To convert a fraction, repeatedly double it and peel off the integer part. For 0.1:
The integer parts give the bits , and once reappears the cycle repeats forever:
This is the rule proved in why we use base 10: a fraction in lowest terms terminates in base exactly when every prime factor of its denominator divides . 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
slightly more than one tenth. The stored 0.2 is also slightly high and the stored 0.3 slightly low, at . Adding 0.1 and 0.2 and rounding gives
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 bits distinguish possibilities, so choosing one of equally likely items takes bits; twenty questions single out one of , 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 megabytes per second. In SI, kilo means 1000, but because 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 , mebi (Mi) for , gibi (Gi) for . A drive sold as 1 TB holds bytes, which is 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.