Binary Calculator: Binary Arithmetic, Bitwise Operations and Two's Complement
By FindMySum · Last reviewed
A binary calculator does arithmetic directly in base 2, the number system every digital computer uses. This one adds, subtracts, multiplies and divides binary numbers of up to 128 digits exactly, runs the bitwise operations programmers use every day (AND, OR, XOR, NOT and shifts), and shows each result in decimal and hexadecimal and as a two's-complement bit pattern at 8, 16, 32 or 64 bits, with a warning when the result overflows that width. A step-by-step table lines the bits up the way you would on paper.
Binary in one paragraph
Each binary digit (bit) is worth a power of 2, doubling from the right: 1, 2, 4, 8, 16 and so on. So 1011 is 8 + 0 + 2 + 1 = 11, and 110 is 4 + 2 + 0 = 6. Hexadecimal groups bits in fours (0000 = 0 up to 1111 = F), which is why programmers write 8-bit values as two hex digits.
Binary arithmetic
Addition works like decimal addition with only two digits. The rules are 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1).
The calculator's default is 1011 + 110:
carry 111
1011
+ 110
= 10001
Reading right to left: 1 + 0 = 1; 1 + 1 = 0 carry 1; 0 + 1 + 1 = 0 carry 1; 1 + 0 + 1 = 0 carry 1; and the last carry becomes the leading 1. The answer 10001 is 16 + 1 = 17, and 11 + 6 = 17 confirms it. Hexadecimal: 0x11.
Subtraction can be done by borrowing (10 − 1 = 1 in a column), but computers add the two's complement instead (see below). If the second number is larger, the answer is negative: 101 − 1100 is 5 − 12 = −111 (−7).
Multiplication is shift-and-add: for every 1 in the second number, add a copy of the first number shifted left by that position. 1101 × 101 = 1101 + 110100 = 1000001 (13 × 5 = 65).
Division gives a quotient and a remainder. 11011 ÷ 101 is 27 ÷ 5 = 101 remainder 10 (5 remainder 2). With negative numbers the calculator follows the rule used by C, Java, JavaScript and most processors: the quotient is truncated toward zero and the remainder has the sign of the dividend, so −27 ÷ 5 gives −5 remainder −2.
Two's complement: how negative numbers are stored
A register has a fixed number of bits, and there is no separate place for a minus sign. Two's complement solves this: in w bits, a negative number −x is stored as the pattern for 2^w − x. A quick way to find it is to invert every bit and add 1:
- 7 in 8 bits is 00000111.
- Invert: 11111000.
- Add 1: 11111001, which is how −7 is stored. (Check: 256 − 7 = 249 = 11111001.)
The top bit acts as the sign: patterns starting with 0 are 0 to 127, patterns starting with 1 are −128 to −1. The big advantage is that ordinary binary addition gives the right answer for signed numbers too, so the same circuit adds signed and unsigned values.
The calculator shows every result's pattern at the bit width you choose, and reads that pattern both ways: as an unsigned number and as a signed (two's complement) number.
Overflow
Every bit width has limits. In 8 bits, unsigned values run from 0 to 255 and signed values from −128 to 127. A result outside the range overflows: the register keeps only the lowest 8 bits. For example, 1111111 + 1 is 127 + 1 = 128. That is fine as an unsigned 8-bit value, but as a signed value the pattern 10000000 means −128: adding two positive numbers produced a negative one. The calculator gives the exact result, the pattern an 8-bit register would actually hold, and whether the result fits as signed, unsigned, both or neither.
Bitwise operations
Bitwise operations work on each bit position independently, on the fixed-width patterns:
| Operation | Rule per bit | 11001100 with 10101010 |
|---|---|---|
| AND | 1 if both bits are 1 | 10001000 |
| OR | 1 if either bit is 1 | 11101110 |
| XOR | 1 if exactly one bit is 1 | 01100110 |
| NOT (of the first) | flip the bit | 00110011 |
Shifts slide the whole pattern:
- Left shift by n moves bits toward the high end and fills with 0s, multiplying by 2ⁿ; bits pushed past the width are lost.
11000001 << 2in 8 bits is 00000100. - Logical right shift moves bits toward the low end and fills with 0s:
10000000 >>> 2= 00100000 (128 → 32). - Arithmetic right shift fills with copies of the sign bit, which divides a signed number by 2ⁿ, rounding down:
10000000 >> 2= 11100000 (−128 → −32).
Operands for bitwise operations must fit in the chosen width (as signed or unsigned); if one doesn't, pick a wider width.
Hexadecimal arithmetic
Hexadecimal (base 16) is binary written four bits at a time: each hex digit 0–9 or A–F stands for one group of four bits, so 1111 0000 is F0 and a byte is always two hex digits. That makes it the usual way to write memory addresses, colours and machine words, and it is why the calculator can do hex arithmetic as well. Choose hexadecimal under "Numbers typed in" and the inputs are read as hex; the operations are the same, and the result is shown in hex first, then binary and decimal.
The rules mirror decimal arithmetic with a carry at 16 instead of 10. Adding 1F and A: F + A is 15 + 10 = 25, which is 1 sixteen and 9 left over, so write 9 and carry 1; then 1 + 1 = 2, giving 29 (41 in decimal). DEADBEEF + 1 carries once, to DEADBEF0. Multiplying 1A by 2B gives 45E (26 × 43 = 1,118), and FF ÷ 10 gives F with a remainder of F (255 ÷ 16 = 15 remainder 15). Bitwise operations are easiest to see in hex: F0 AND 3C keeps only the bits set in both, 30. Up to 32 hex digits are accepted, the same 128 bits as the binary input.
Limits
The calculator handles whole numbers only. It doesn't do binary fractions (such as 101.11), IEEE 754 floating-point formats or bit rotations. Arithmetic is exact for inputs up to 128 binary digits; bit patterns are shown for 8, 16, 32 and 64 bits.
More questions
Why do computers use binary?
Because each digit only needs two states, on or off, which transistors represent reliably and cheaply. Every number, character and instruction in a computer is ultimately a pattern of bits. Hexadecimal is used as shorthand because each hex digit stands for exactly four bits: 1111 1001 is F9.
How do I convert a binary number to decimal by hand?
Write the powers of 2 under the digits from the right (1, 2, 4, 8, 16, …) and add the powers where the digit is 1. For 10001: 16 + 1 = 17. To go the other way, divide by 2 repeatedly and read the remainders from the bottom up. The number system converter does this for any base from 2 to 36.
Can I add or multiply hexadecimal numbers?
Yes. Set "Numbers typed in" to hexadecimal and type A and B with the digits 0–9 and A–F (a 0x prefix is fine). Every operation, from addition and division to AND, OR and the shifts, then runs on their values, and the result is shown in hex, binary and decimal. For example 1F + A = 29 in hex, which is 41 in decimal.
What is the range of an n-bit number?
Unsigned, 0 to 2ⁿ − 1: 255 for 8 bits, 65,535 for 16, about 4.29 billion for 32 and about 1.8 × 10¹⁹ for 64. Signed two's complement, −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: −128 to 127 for 8 bits, −32,768 to 32,767 for 16, about ±2.15 billion for 32 and about ±9.2 × 10¹⁸ for 64.
What is sign extension?
Copying the sign bit into the new high bits when a two's-complement number is widened, so its value doesn't change. −7 is 11111001 in 8 bits and 1111111111111001 in 16 bits. Positive numbers are padded with 0s. Switch the bit width in the calculator to see the pattern of the same value at 8, 16, 32 and 64 bits.
What are bitwise operations used for in practice?
Masks and flags: AND with a mask keeps selected bits (a subnet mask ANDed with an IP address gives the network address), OR sets bits, XOR toggles bits and is used in checksums and simple ciphers, and shifts multiply or divide by powers of two quickly. File permissions such as 755 on Linux are three groups of three permission bits.
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.