Binary Calculator
Perform arithmetic operations on binary numbers.
Related calculators
About
Binary Calculator
The binary system works almost exactly like the decimal system most people grew up with. Decimal uses 10 as its base and the digits 0 through 9; binary uses base 2 and only the digits 0 and 1. Each binary digit is called a bit. Past that difference, addition, subtraction, multiplication and division follow the same rules in both.
Almost all modern computing runs on binary, because two states are far easier to build in circuitry than ten. A logic gate only has to tell on from off, true from false, current from no current. A decimal machine would need hardware that reliably distinguishes ten separate voltage levels, which is harder to build and much easier to get wrong.
Here are some typical conversions between the two:
| Decimal | Binary |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 7 | 111 |
| 8 | 1000 |
| 10 | 1010 |
| 16 | 10000 |
| 20 | 10100 |
Place values
Binary looks strange at first, but each place represents 2n in exactly the way each decimal place represents 10n. Take the number 8 in decimal. It sits in the first place left of the decimal point, the 100 place, so it means:
8 × 100 = 8 × 1 = 8
Using 18 for comparison:
(1 × 101) + (8 × 100) = 10 + 8 = 18
In binary, 8 is written 1000. Reading from the right, the first place is 20, the second 21, the third 22 and the fourth 23, just like the decimal system with a base of 2 instead of 10. Since 23 = 8, a 1 goes in that position and 1000 is the result. Taking 18, which is 10010:
18 = 16 + 2 = 24 + 21
10010 = (1 × 24) + (0 × 23) + (0 × 22) + (1 × 21) + (0 × 20) = 18
Decimal to binary, step by step
- Find the largest power of 2 that fits inside the number.
- Subtract that value from the number.
- Find the largest power of 2 inside the remainder from step 2.
- Repeat until there is no remainder.
- Write a 1 in every binary place that was used, and a 0 in the rest.
With 18 again, another way to see it:
| 2n | 24 | 23 | 22 | 21 | 20 |
|---|---|---|---|---|---|
| Value | 16 | 8 | 4 | 2 | 1 |
| Instances within 18 | 1 | 0 | 0 | 1 | 0 |
| Left over | 18 − 16 = 2 | 2 | 2 | 2 − 2 = 0 | 0 |
Binary to decimal
This direction is easier. Find every place where a 1 sits and add those place values together.
10111 = (1 × 24) + (0 × 23) + (1 × 22) + (1 × 21) + (1 × 20) = 23
| 2n | 24 | 23 | 22 | 21 | 20 |
|---|---|---|---|---|---|
| Digit | 1 | 0 | 1 | 1 | 1 |
| Value added | 16 | 0 | 4 | 2 | 1 |
16 + 4 + 2 + 1 = 23
Binary addition
Binary addition follows the same rules as decimal addition, except that a column carries when it reaches 2 rather than 10. The whole rule set is four lines:
0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0, carry the 1, which is 10
Adding 1101 and 10111, with the carries written above:
1 1 1 1 1 0 0 1 1 0 1 + 0 1 0 1 1 1 ----------- = 1 0 0 1 0 0
In decimal that is 13 + 23 = 36, and 100100 is 32 + 4 = 36. The one thing to watch is the column where 1 + 1 = 0 and a 1 has been carried in from the right. The digit written down is then 1, not 0, and another 1 still carries onward. That case appears in the third column from the right above.
Binary subtraction
Subtraction differs from decimal only in what the borrowing looks like. Borrowing is needed whenever the digit being subtracted is larger than the one it is taken from, which in binary means exactly one case: 1 taken from 0. The 0 becomes a 2, so 2 − 1 = 1, and the column lent from drops by 1. If that neighbour is also a 0, the borrow travels along until it reaches a column holding a 1.
0 - 0 = 0 0 - 1 = 1, borrowing 1 from the next column 1 - 0 = 1 1 - 1 = 0
Taking 1101 from 10111:
1 0 1 1 1 - 0 1 1 0 1 --------- = 0 1 0 1 0
That is 23 − 13 = 10, and 1010 is 8 + 2 = 10. A shorter one, 100 − 011, shows the borrow travelling:
1 0 0 - 0 1 1 ----- = 0 0 1
4 − 3 = 1. The rightmost column needs to take 1 from 0, and its neighbour is also 0, so the borrow has to reach all the way to the leading 1.
Binary multiplication
Multiplication is arguably easier in binary than in decimal, because each partial product is either a copy of the first value or nothing at all:
0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1
Each row shifts one place further left, exactly as in decimal long multiplication, and the rows are then added together. Multiplying 10111 by 11:
1 0 1 1 1
x 1 1
-------------
1 0 1 1 1
+ 1 0 1 1 1 0
-------------
= 1 0 0 0 1 0 1
23 × 3 = 69, and 1000101 is 64 + 4 + 1 = 69. The difficulty in binary multiplication is never the multiplying, which is trivial; it is the long addition at the end, which grows with the number of bits.
Note that the 0 placeholder is written out in the second row. Decimal long multiplication usually leaves it implied, but writing it keeps the columns honest when the rows are added. On the same subject: a 0 to the right of a 1 changes the value, while a 0 to the left of the leading 1 does not.
1 0 1 0 1 1 0 0 = 0 0 1 0 1 0 1 1 0 0 (same value, leading zeros are padding) # 1 0 1 0 1 1 0 0 0 0 (different value, trailing zeros shift it)
Binary division
Binary division is long division with binary subtraction inside it. The dividend is divided by the divisor exactly as in decimal, and a solid grasp of binary subtraction is what makes it manageable. There is one simplification: at each step the divisor either fits or it does not, since it can never fit more than once. That makes every quotient digit a straight yes or no.
0 0 1 1 1
11 ) 1 0 1 0 1
1 1
-----
1 0 0
1 1
---
1 1
1 1
---
0
10101 ÷ 11 = 111, which is 21 ÷ 3 = 7 with no remainder. Where a division does not come out evenly, what is left at the bottom is the remainder, and the calculator reports it in both binary and decimal.
Where binary shows up
Eight bits make a byte, which is why so many limits in computing are 255. Eight bits hold 28 = 256 different values, numbered 0 to 255, so a colour channel in a hex code runs to FF, and each part of an IPv4 address stops at 255.
The same powers explain file sizes. A kibibyte is 210 = 1,024 bytes rather than 1,000, and a mebibyte is 220 = 1,048,576. It also explains why 32-bit systems hit a wall at 4 GB: 232 = 4,294,967,296 distinct addresses. Moving to 64 bits raises that to 264 = 18,446,744,073,709,551,616, which is why nobody is worrying about the next wall.
Binary is also the reason 0.1 cannot be stored exactly in most programming languages. One tenth in binary repeats forever, the same way one third repeats forever in decimal, so it gets cut off and rounded. That single fact is behind a great many reports of a program adding 0.1 and 0.2 and producing 0.30000000000000004.
Common mistakes
The first is dropping a carry during addition. When a column produces 1 + 1 = 0 and something has already been carried in, the answer for that column is 1 and a carry still goes out. Missing it puts every column to the left out by one.
The second is reading binary from the wrong end. Place values start at 20 on the right, not the left, so 10 means 2 and not 1.
The third is assuming leading zeros matter. 0010 and 10 are the same number. Fixed-width formats pad with leading zeros to fill a byte, which is presentation rather than value, and the calculator prints the value without padding.
Common questions
Frequently asked questions
Column by column from the right, carrying whenever a column reaches 2 instead of 10. The four rules are 0+0=0, 0+1=1, 1+0=1 and 1+1=0 with a 1 carried. For example 1101 + 10111 = 100100, which is 13 + 23 = 36.
Add up the place value everywhere a 1 appears, counting from 2 to the power 0 on the right. So 10111 is 16 + 4 + 2 + 1 = 23. It is the easier of the two directions.
Find the largest power of 2 that fits, subtract it, then repeat on what is left until nothing remains. Write a 1 in every place used and a 0 elsewhere. For 18 that is 16 and 2, giving 10010.
Because a circuit only has to tell two states apart: on or off, current or no current. Building hardware that reliably distinguishes ten voltage levels for the digits 0 to 9 is much harder and far easier to get wrong.
It is needed in one case only, when 1 is taken from 0. The 0 becomes a 2, so 2 minus 1 is 1, and the column lent from drops by 1. If that neighbour is also 0, the borrow carries on until it reaches a 1. In 100 minus 011 the borrow has to travel all the way to the leading 1.
The multiplying is, since every partial product is either a copy of the first value or nothing. The work moves to the addition at the end, which grows with the number of bits. 10111 times 11 is 1000101, which is 23 times 3 = 69.
As long division with binary subtraction inside it. At each step the divisor either fits or it does not, because it can never fit more than once, so every quotient digit is a yes or no. 10101 divided by 11 is 111, which is 21 divided by 3 = 7.
No. 0010 and 10 are both 2. Zeros to the left of the leading 1 are padding used to fill a fixed width such as a byte. Zeros to the right of a 1 do change the value, since they shift it into a higher place.
A bit is a single binary digit, 0 or 1. Eight bits make a byte, which holds 2 to the power 8 = 256 values numbered 0 to 255. That limit is why colour channels stop at 255 and why each part of an IPv4 address does too.
One tenth repeats forever in binary the way one third repeats forever in decimal, so it has to be cut off and rounded. That is why adding 0.1 and 0.2 in many languages returns 0.30000000000000004 rather than 0.3.