Binary calculator
A binary addition, subtraction, multiplication and division calculator, plus bitwise operations, with the result in hex and decimal too.
Binary addition
Add column by column from the right, as with decimal. The only sums are 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1).
For example, 1011 + 110: the right column is 1 + 0 = 1; then 1 + 1 = 10, write 0 and carry 1; then 0 + 1 + 1 = 10, write 0 and carry 1; then 1 plus the carry is 10 again, so write 0 and carry 1 into a new column. The answer is 10001, which checks out in decimal: 11 + 6 = 17.
1011 + 110 ───── 10001
Binary subtraction
Subtract column by column from the right. 1 − 0 = 1, 1 − 1 = 0 and 0 − 0 = 0. For 0 − 1, borrow 1 from the next column to the left: the 0 becomes 10 (two), and 10 − 1 = 1.
For example, 1011 − 110 = 101, which is 11 − 6 = 5 in decimal.
Computers usually subtract by adding instead. In 8 bits, −6 is stored as the two’s complement of 00000110, which is 11111010. Adding it to 00001011 gives 1 00000101; drop the carry out of the top bit and 00000101 is 5.
1011 − 110 ──── 101
Binary multiplication
Multiply the way you would on paper, but each step is simpler: multiplying by a 1 copies the top number, and multiplying by a 0 gives 0. Shift each copy one place left for each column, then add the copies.
For 1011 × 110, the three digits of 110 give 0, 10110 and 101100. Adding them gives 1000010, which is 11 × 6 = 66.
Division is the reverse: repeated subtraction of shifted copies. The calculator gives the whole-number quotient, and mod gives the remainder. The binary arithmetic guide works through all four operations, including long division.
1011 × 110 ─────── 1000010
Bitwise operations
| Operation | Rule | 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 |
| Shift left by 1 | Move bits left, add a 0; doubles the value | 11000 |
| Shift right by 1 | Move bits right, drop the last; halves and rounds down | 110 |
Bit widths and overflow
Real hardware works in fixed widths. Choose 8, 16, 32 or 64 bits and the calculator wraps the result to that width, like a CPU register, and flags unsigned overflow (the result didn’t fit) and signed overflow (the sign came out wrong). For example, 11111111 + 1 in 8 bits is 00000000: an unsigned overflow, but no signed overflow, because it’s −1 + 1 = 0.
How it’s calculated
Results use exact whole-number arithmetic at any size. With a bit width chosen, the result is wrapped to that width the way a CPU register does, and overflow is checked separately for unsigned and signed (two’s complement) readings.
Questions
How does the calculator handle negative results?
With no width selected it shows a minus sign, like −10. With a width selected it shows the two’s complement bit pattern and the signed value next to it.
How does binary division work here?
It’s whole-number division: the result is rounded towards zero, and mod gives the remainder.
Can I type in hex or decimal?
Switch the number base at the top. The hex calculator has the same operations with hex input.