Binary Calculator: Add, Subtract, AND, OR, XOR and Shift
11001010 + 01100111 in binary is 0011 0001 in an 8-bit word (hex 31): the true sum is 305, which does not fit in 8 bits, so the carry out of the top bit is lost and the answer wraps to 49. To add in binary, work from the right, add each column of bits plus the carry (1 + 1 = 10, write 0 and carry 1), and drop any carry past the word size. The calculator below also subtracts, multiplies, divides with a remainder, applies AND, OR, XOR, NOT, NAND, NOR and shifts, on binary, octal, decimal or hex numbers, and flags unsigned and signed overflow.
Updated Runs in your browser: nothing you enter is uploadedHow this tool works
Result
Result: 0011 0001 (hex 31) is 49 signed and 49 unsigned. Carry out of the top bit (unsigned overflow): yes. Signed overflow: no.
8-bit word: signed -128 to 127, unsigned 0 to 255. Results wrap around at 256.
- Binary (8 bits)
- 0011 0001
- Octal
- 61
- Decimal, signed
- 49
- Decimal, unsigned
- 49
- Hexadecimal
- 31
Overflow
- Carry out of the top bit (unsigned overflow)
- Yes
- Signed overflow
- No
- Unsigned: 202 + 103 = 305, outside 0 to 255, so it wraps to 49.
- Signed: -54 + 103 = 49, inside -128 to 127, so nothing is lost.
Carry row (long addition)
| Row | Carry outOut | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
|---|---|---|---|---|---|---|---|---|---|
| Carry in | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| A | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | |
| B | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | |
| Sum | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 1 |
Each column adds the bits of A and B and the carry into it; a carry goes into the next column to the left. A carry out of the leftmost column is an unsigned overflow.
Bit grid
8-bit two’s complement, most significant nibble first.
| Bits | A | B | Result |
|---|---|---|---|
| Bits 7–4 | 1100 | 0110 | 0011 |
| Bits 3–0 | 1010 | 0111 | 0001 |
Bitwise operations, bit by bit
| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Range of each word size
| Word size | Unsigned | Signed | Largest value, in hex | Most negative, in hex |
|---|---|---|---|---|
| 8-bit | 0 to 255 | -128 to 127 | FF | 80 |
| 16-bit | 0 to 65,535 | -32,768 to 32,767 | FF FF | 80 00 |
| 32-bit | 0 to 4,294,967,295 | -2,147,483,648 to 2,147,483,647 | FF FF FF FF | 80 00 00 00 |
| 64-bit | 0 to 18,446,744,073,709,551,615 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | FF FF FF FF FF FF FF FF | 80 00 00 00 00 00 00 00 |
When an 8-bit result overflows
| Calculation | Result bits | Unsigned reading | Signed reading | Unsigned overflow | Signed overflow |
|---|---|---|---|---|---|
| 100 + 27 | 0111 1111 | 127 | 127 | No | No |
| 127 + 1 | 1000 0000 | 128 | -128 | No | Yes |
| 200 + 100 | 0010 1100 | 44 | 44 | Yes | No |
| 255 + 1 | 0000 0000 | 0 | 0 | Yes | No |
| -128 − 1 | 0111 1111 | 127 | 127 | No | Yes |
| 0 − 1 | 1111 1111 | 255 | -1 | Yes | No |
| 16 × 16 | 0000 0000 | 0 | 0 | Yes | Yes |
| -1 × -1 | 0000 0001 | 1 | 1 | Yes | No |
Shifts of 1011 0100 in 8 bits
| Shift | Result bits | Signed value | Bits shifted out |
|---|---|---|---|
| Left by 2 (<<) | 1101 0000 | -48 | Yes |
| Logical right by 2 (>>>) | 0010 1101 | 45 | No |
| Arithmetic right by 2 (>>) | 1110 1101 | -19 | No |
| Arithmetic right by 7 (>>) | 1111 1111 | -1 | Yes |
Bitwise results for 1100 1010 and 0110 0111
| Operation | Result bits | Hex |
|---|---|---|
| AND | 0100 0010 | 42 |
| OR | 1110 1111 | EF |
| XOR | 1010 1101 | AD |
| NAND | 1011 1101 | BD |
| NOR | 0001 0000 | 10 |
How to add, subtract and shift binary numbers
- Add in binary. Line the numbers up on the right and add column by column from the right: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1), and 1 + 1 + 1 = 11 (write 1, carry 1). For 11001010 + 01100111 the columns give 00110001 with a carry out of the top bit, so in 8 bits the answer is 0011 0001.
- Subtract with two’s complement. To compute A − B, flip every bit of B, add 1, and add that to A, dropping the carry out. In 8 bits, 5 − 3 is 0000 0010 (2), and 3 − 5 gives 1111 1110, which reads as -2 signed.
- Apply AND, OR, XOR, NOT. Compare the numbers bit by bit in the same position. AND is 1 only if both bits are 1, OR is 1 if either is, XOR is 1 if they differ, and NOT flips every bit of the word. 1100 1010 AND 0110 0111 is 0100 0010; the same pair gives 1010 1101 with XOR.
- Shift bits left or right. A left shift by n moves every bit n places up and fills with zeros, which multiplies by 2ⁿ until bits fall off the top. A logical right shift fills the top with zeros; an arithmetic right shift copies the sign bit. 1011 0100 shifted right by 2 is 0010 1101 logically and 1110 1101 arithmetically.
- Choose a word size and read the flags. Pick 8, 16, 32 or 64 bits to behave like a register of that size; every result wraps at 2ⁿ. The calculator shows both readings of the result bits and says whether the unsigned result overflowed (a carry or borrow) or the signed result did. Choose Unlimited for exact arithmetic with no wrap-around.
Frequently asked questions
- How do I add two binary numbers?
- Add them column by column from the right, carrying 1 into the next column whenever a column adds to 2 or more: 1 + 1 is 10 in binary. 11001010 + 01100111 gives 0011 0001 in 8 bits, with a carry out of the top bit. The carry row under the calculator shows the carry into every column.
- What is two’s complement?
- Two’s complement is how computers store negative whole numbers in a fixed number of bits: −x is stored as 2ⁿ − x, which is the same as flipping every bit of x and adding 1. In 8 bits, −5 is 1111 1011. The top bit is 1 for every negative number, and the same adder hardware works for signed and unsigned numbers. The textbook treatment is in Patterson and Hennessy, Computer Organization and Design, section 2.4 (signed and unsigned numbers).
- What is the difference between a carry and an overflow?
- A carry (or borrow) means the unsigned result did not fit: 255 + 1 in 8 bits carries out and wraps to 0. A signed overflow means the signed result did not fit: 127 + 1 is -128 when read as signed. They are different tests on the same bits, which is why a CPU has separate carry and overflow flags (CF and OF on x86).
- What is the difference between a logical and an arithmetic right shift?
- A logical right shift (>>>) fills the vacated top bits with 0, which treats the bits as an unsigned number and divides by 2 per place. An arithmetic right shift (>>) copies the sign bit instead, so a negative number stays negative: 1011 0100 (−76) shifted right by 2 is 1110 1101 (-19) arithmetically but 0010 1101 (45) logically. A left shift is the same for both.
- How does division work, and what about the remainder?
- The calculator gives an integer quotient and a remainder, with the quotient cut toward zero and the remainder taking the sign of A, as in C, Java and JavaScript: −7 ÷ 2 is −3 remainder −1. Choose whether the bits are read as signed or unsigned: the bits 1111 1001 are −7 signed but 249 unsigned, so they divide differently. Dividing by 0 is reported as an error.
- What do NAND and NOR do?
- NAND is AND followed by NOT, and NOR is OR followed by NOT, applied to every bit of the word. For 1100 1010 and 0110 0111, NAND is 1011 1101 and NOR is 0001 0000. Either one alone can build every other logic gate.
- What does Unlimited word size mean?
- Unlimited uses exact integers of any size (JavaScript BigInt) with no wrap-around, so 2^64 − 1 plus 1 is 18,446,744,073,709,551,616. Negative numbers act as if they had endless 1 bits in front, which is how bitwise operators on negative integers behave in Python and JavaScript. A logical right shift of a negative number is refused, because it needs a fixed width.
- Can I mix bases, like hex plus binary?
- Yes: each number has its own base selector (binary, octal, decimal or hex), so 0xFF plus 0b1 works. Spaces and underscores between digits are ignored, and you can type a minus sign or the matching prefix (0x, 0b, 0o). The result is shown in all four bases. Only whole numbers are supported, and each must fit the chosen word size.
How it works
Each number is read digit by digit in its base into an exact whole number (JavaScript BigInt), using the same parser as the number base converter. With a fixed word size n, it is stored as an n-bit pattern, the remainder after dividing by 2ⁿ, and accepted if it lies between −2ⁿ⁻¹ and 2ⁿ − 1. Addition, subtraction, multiplication and the bitwise operations then work on those patterns and keep only the low n bits. The unsigned flag compares the exact result with 0 and 2ⁿ − 1; the signed flag reads both operands as two’s complement and compares the exact result with −2ⁿ⁻¹ and 2ⁿ⁻¹ − 1. Division reads the patterns as signed or unsigned as you choose and truncates toward zero. Logical shifts move the pattern, and an arithmetic right shift moves the signed value, copying the sign bit. Shifts by more than the word size are refused, because C gives such a shift no defined result. With an unlimited word size nothing wraps, and negative values follow the endless two’s complement convention of BigInt. The carry row repeats the textbook full-adder rule: the carry out of a column is 1 when at least two of its three input bits are 1.
Sources
- Patterson and Hennessy, Computer Organization and Design, section 2.4: Signed and Unsigned Numbers (two’s complement)
- Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: integer data types and the CF and OF status flags
- Two’s complement, Wikipedia
- Bitwise operations in JavaScript, MDN Web Docs
More free tools
- Text to Binary ConverterConvert text to binary, hex, octal or decimal bytes and back. UTF-8 by default (é is 2 bytes, an emoji 4) or UTF-16, with a per-character breakdown.
- Hex CalculatorAdd, subtract, multiply, divide and AND, OR, XOR, NOT or shift hex numbers of any size, with 8 to 64-bit overflow, two’s complement and long-hand working.
- Chmod CalculatorConvert Unix file permissions between a checkbox grid, octal (755, 4755), ls -l text (rwxr-xr-x) and chmod commands; apply changes like u+x,go-w.
- IP Subnet CalculatorWork out the network, broadcast, usable hosts, masks and binary of any IPv4 CIDR block, split it into subnets, and expand or compress IPv6 addresses.
- Data Storage ConverterConvert bits, bytes, KB, MB, GB, TB, PB and KiB, MiB, GiB, TiB, PiB exactly, and see why a 1 TB drive shows about 931 GB in Windows.
- ASCII TableAll 128 ASCII codes with decimal, hex, octal, binary, character, HTML entity and control-character names. Look up a character, number or name like tab.
More: Encoding and number systemsAll developer toolsAll toolsHome & DIY calculators