Skip to main content
ponderloop

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

Whole numbers only. Spaces, underscores, a minus sign and a 0x, 0b or 0o prefix matching the base are fine to type. Every result shows both readings of its bits, signed and unsigned.

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)

Long addition in binary, one column per bit, most significant bit on the left
RowCarry outOutBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
Carry in110011100
A11001010
B01100111
Sum00110001

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 of each value, four at a time
BitsABResult
Bits 7–4110001100011
Bits 3–0101001110001

Bitwise operations, bit by bit

The result for every pair of input bits, computed by the calculator at one bit. NOT flips a single bit.
ABANDORXORNANDNOR
0000011
0101110
1001110
1111000

Range of each word size

The same n bits read two ways: unsigned (0 to 2ⁿ − 1) or signed two’s complement (−2ⁿ⁻¹ to 2ⁿ⁻¹ − 1).
Word sizeUnsignedSignedLargest value, in hexMost negative, in hex
8-bit0 to 255-128 to 127FF80
16-bit0 to 65,535-32,768 to 32,767FF FF80 00
32-bit0 to 4,294,967,295-2,147,483,648 to 2,147,483,647FF FF FF FF80 00 00 00
64-bit0 to 18,446,744,073,709,551,615-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807FF FF FF FF FF FF FF FF80 00 00 00 00 00 00 00

When an 8-bit result overflows

Unsigned overflow (carry or borrow) and signed overflow are separate: the same bits can overflow one reading and not the other.
CalculationResult bitsUnsigned readingSigned readingUnsigned overflowSigned overflow
100 + 270111 1111127127NoNo
127 + 11000 0000128-128NoYes
200 + 1000010 11004444YesNo
255 + 10000 000000YesNo
-128 − 10111 1111127127NoYes
0 − 11111 1111255-1YesNo
16 × 160000 000000YesYes
-1 × -10000 000111YesNo

Shifts of 1011 0100 in 8 bits

A left shift multiplies by 2 and drops bits off the top. A logical right shift fills with 0; an arithmetic right shift copies the sign bit, so a negative number stays negative.
ShiftResult bitsSigned valueBits shifted out
Left by 2 (<<)1101 0000-48Yes
Logical right by 2 (>>>)0010 110145No
Arithmetic right by 2 (>>)1110 1101-19No
Arithmetic right by 7 (>>)1111 1111-1Yes

Bitwise results for 1100 1010 and 0110 0111

OperationResult bitsHex
AND0100 001042
OR1110 1111EF
XOR1010 1101AD
NAND1011 1101BD
NOR0001 000010

How to add, subtract and shift binary numbers

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

More: Encoding and number systemsAll developer toolsAll toolsHome & DIY calculators