Number Theory & Bases

Binary Calculator

This binary calculator adds, subtracts, multiplies and divides binary numbers with steps, and runs bitwise AND, OR, XOR, NOT and shifts.

Try
Input format

In the input format chosen above. A minus sign means two’s complement; spaces between digit groups are fine.

In the same format as A.

Word size
Read numbers as

Result

1101 + 1011 (8-bit): 0001 1000

1101 + 1011 (8-bit)

0001 1000

24 in decimal (unsigned)

Binary
0001 1000
Hexadecimal
18
Octal
30
Decimal (unsigned)
24
Decimal (signed, two’s complement)
24
True result (no wrapping)Fits in 8 bits (unsigned)
24
Unsigned overflow (carry)
No
Signed overflow
No
Bits set to 1
2

A + B gives 0001 1000 in 8 bits: 24 unsigned, and the same signed.

A + B in an 8-bit word
ValueBinaryHexUnsignedSigned
A0000 11010D1313
B0000 10110B1111
Result0001 1000182424
Step-by-step working (3 steps)
  1. Write the inputs as 8-bit patterns

    A = 13 → 0000 1101; B = 11 → 0000 1011. Shorter inputs are padded with zeros on the left.

  2. Add column by column

    Starting from the right, 1 + 1 = 10 in binary: write 0 and carry 1. 0000 1101 + 0000 1011 = 0001 1000. The carries out of each column were 0000 1111.

  3. Read the result

    The top bit is 0, so the pattern means 24 whether it is read as signed or unsigned.

Type values in binary, octal, decimal or hex and pick an 8, 16, 32 or 64-bit word. Negative numbers use two’s complement, −x=NOTx+1-x = \mathrm{NOT}\,x + 1, and overflow is flagged.

By Hira Noor, mathematicianLast reviewed Free, no sign-up, runs in your browserReport a problem

How to use this calculator

  1. Pick the input format, then type A (and B) in that format, for example 1101 in binary or F0F0 in hex. A minus sign stores the value in two’s complement.
  2. Choose the operation. Shifts ask for the number of bit positions instead of B.
  3. Choose the word size and whether to read the bits as unsigned or signed numbers. This changes division, the overflow check and the decimal result.
  4. Read the answer at the top in your input format. The steps show the working, and the list and table give binary, hex, octal and decimal. A warning appears if the true result does not fit.

Formulas

Two’s complement

−x≡NOTx+1;(mod;2w),value=−bw−12w−1+∑i=0w−2bi2i-x \equiv \mathrm{NOT}\,x + 1 ;(\mathrm{mod}; 2^{w}),\qquad \text{value} = -b_{w-1} 2^{w-1} + \sum_{i=0}^{w-2} b_i 2^{i}

Plain-text formula: −x ≡ NOT x + 1;(mod ;2^w), value = −b_(w − 1)2^(w − 1) + ∑_{i = 0}^(w − 2) b_i2^i

ww
word size in bits (8, 16, 32 or 64)
bib_i
bit i, with bw−1b_{w-1} the sign bit

In a w-bit word the top bit counts as −2^(w−1) instead of +2^(w−1). To negate a number, invert every bit and add 1. The same adder then handles positive and negative numbers.

Ranges of a w-bit word

unsigned: 0 to 2w−1,signed: −2w−1 to 2w−1−1\text{unsigned: } 0 \text{ to } 2^{w} - 1,\qquad \text{signed: } -2^{w-1} \text{ to } 2^{w-1} - 1

Plain-text formula: unsigned: 0 to 2^w − 1, signed: − 2^(w − 1) to 2^(w − 1) − 1

For 8 bits that is 0 to 255 unsigned and −128 to 127 signed. For 32 bits it is −2,147,483,648 to 2,147,483,647 signed.

Shifts

x≪n=x⋅2nmod⁡2w,x≫n=⌊x/2n⌋x \ll n = x \cdot 2^{n} \mod 2^{w},\qquad x \gg n = \left\lfloor x / 2^{n} \right\rfloor

Plain-text formula: x≪n = x · 2^n mod 2^w, x≫n = ⌊x/2^n⌋

A logical right shift fills with zeros, which divides an unsigned number. An arithmetic right shift copies the sign bit, which divides a signed number and rounds toward minus infinity.

Reading the result

The same bit pattern can mean two numbers. 1111 1011 is 251 unsigned and −5 signed. The hardware stores identical bits; only the instructions that read them differ. Addition, subtraction, the low bits of a product, AND, OR, XOR and left shifts give the same bits either way. Division, right shifts, comparisons and overflow depend on the reading.

Overflow means the true result needs more bits than the word has. Unsigned overflow shows up as a carry out of the top bit, or a borrow when subtracting. Signed overflow happens when two numbers of the same sign give a result of the other sign, such as 127 + 1 = −128 in 8 bits. Many languages wrap silently, so these warnings point to real bugs.

Bits and bytes also size files and memory. Storage uses powers of 10 (1 GB = 10⁹ bytes) and powers of 2 (1 GiB = 2³⁰ bytes), which is why a drive shows less space than its label: see the gigabytes to gibibytes converter.

How to add binary numbers

Binary addition works like decimal addition, column by column from the right, with only four rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1). The carry moves one column left, just as a carry of ten does in decimal.

For example, 1101 + 1011 is 13 + 11. The table shows each column, starting from the right. The answer is 11000, which is 24. Adding 1 + 1 + 1 + 1 gives 100, the binary form of 4. To use this page as a binary addition calculator, choose A + B: the steps list the carry out of every column.

Adding 1101 + 1011 column by column
Column (from right)Bits addedSumWriteCarry
1st (1s)1 + 11001
2nd (2s)0 + 1 + carry 11001
3rd (4s)1 + 0 + carry 11001
4th (8s)1 + 1 + carry 11111
5th (16s)carry 1110

How to subtract binary numbers

Binary subtraction works column by column from the right. The rules are 0 − 0 = 0, 1 − 0 = 1, 1 − 1 = 0 and 0 − 1 = 1 with a borrow from the next column. A borrow turns the 0 into 10, which is 2, and the column you borrowed from loses 1.

For example, 1101 − 0110 (13 − 6): the 1s column gives 1 − 0 = 1. The 2s column needs a borrow: 10 − 1 = 1. The 4s column now holds 0, so it borrows too: 10 − 1 = 1. The 8s column is left with 0 − 0 = 0. The answer is 0111, which is 7.

Computers subtract a different way: they add the two’s complement of B. Invert every bit of B, add 1, then add the result to A and drop any carry out of the top bit. Choose A − B in the calculator to see the borrows and this check step by step.

Binary multiplication and division

To multiply, write a copy of A for every 1 bit in B, shifted left by that bit’s position, then add the copies. For 101 × 11 (5 × 3), the copies are 101 and 1010, and 101 + 1010 = 1111, which is 15. Shifting left by one place always doubles a number.

Binary long division follows the decimal method, but each quotient digit is 0 or 1: the divisor either fits under the current part of the dividend or it does not. For 1101 ÷ 101 (13 ÷ 5) the quotient is 10 and the remainder is 11, that is 2 remainder 3. Choose A ÷ B to get the quotient and remainder in every base.

Binary place values: converting to and from decimal

Each binary digit is worth twice the digit to its right. To read a binary number in decimal, add the place values where the bit is 1. For example, 0010 1101 = 32 + 8 + 4 + 1 = 45, and 1111 1111 = 255, the largest 8-bit unsigned number.

To convert decimal to binary, divide by 2 repeatedly and write down the remainders, then read them from the last to the first. For 45: 45 ÷ 2 = 22 r 1, 22 ÷ 2 = 11 r 0, 11 ÷ 2 = 5 r 1, 5 ÷ 2 = 2 r 1, 2 ÷ 2 = 1 r 0, 1 ÷ 2 = 0 r 1. Reading upward gives 101101. For bases from 2 to 36 and fractions, use the number base converter.

Place values of an 8-bit number, with 45 as an example
Bit76543210
Place value1286432168421
45 in binary00101101
255 in binary11111111

How to find the two’s complement of a binary number

Write the number with the full word size, invert every bit, then add 1. In 4 bits, the two’s complement of 0001 is 1110 + 1 = 1111, so 1111 means −1. In 8 bits, 1111 1111 is −1 as a signed number and 255 as an unsigned one.

To read a signed pattern whose top bit is 1, take its two’s complement and put a minus sign in front. For 1001 0110: invert to 0110 1001, add 1 to get 0110 1010 = 106, so the pattern means −106. Subtracting 256 from its unsigned value, 150, gives the same answer.

Signed and unsigned readings of some 8-bit patterns
BitsUnsignedSigned (two’s complement)
0000 000000
0111 1111127127 (signed maximum)
1000 0000128−128 (signed minimum)
1001 0110150−106
1111 1011251−5
1111 1111255−1

Bitwise operations: AND, OR, XOR and NOT

A bitwise calculator compares two numbers one bit position at a time, with no carries between columns. AND keeps a bit only where both inputs have a 1, so it is used to mask bits off. OR sets a bit where either input has a 1. XOR sets a bit where the inputs differ, so applying the same XOR twice gives back the original. NOT flips every bit.

For example, in hex F0F0 AND 0FF0 = 00F0: only the middle four bits are 1 in both. Shifts move the whole pattern: a left shift by n multiplies by 2ⁿ, and a right shift by n divides by 2ⁿ.

Truth table for one bit of A and B
ABA AND BA OR BA XOR BNOT A
000001
010111
100110
111100

Worked example

Add 100 + 80 as 8-bit signed numbers

Compute 100 + 80 in an 8-bit word and check for overflow.

  1. Write the inputs: 100 = 0110 0100 and 80 = 0101 0000.
  2. Add column by column: 0110 0100 + 0101 0000 = 1011 0100. There is no carry out of the top bit.
  3. Unsigned, 1011 0100 is 180, which fits in 0 to 255, so there is no unsigned overflow.
  4. Signed, the top bit is 1, so the pattern means 180 − 256 = −76. Adding two positive numbers gave a negative one: signed overflow.

Answer: 100 + 80 = 1011 0100 in 8 bits: correct as an unsigned result (180), but a signed overflow that wraps to −76.

Frequently asked questions

How do I add binary numbers?

Add column by column from the right, as in decimal, using 0 + 0 = 0, 0 + 1 = 1 and 1 + 1 = 10 (write 0, carry 1). With a carry, 1 + 1 + 1 = 11. For example 1101 + 1011 = 11000, which is 13 + 11 = 24.

What is 1 + 1 + 1 + 1 in binary?

100. Four is 1 × 4 + 0 × 2 + 0 × 1, so it needs three binary digits. Step by step: 1 + 1 = 10, 10 + 1 = 11, and 11 + 1 = 100.

What is 11111111 in binary?

As an unsigned number, 1111 1111 is 255 (128 + 64 + 32 + 16 + 8 + 4 + 2 + 1), the largest value 8 bits can hold. As a signed 8-bit number in two’s complement, it is −1.

What is two’s complement and how do I calculate it?

It is the standard way computers store negative integers. To negate a number, invert all its bits and add 1. For 5 = 0000 0101, inverting gives 1111 1010 and adding 1 gives 1111 1011, which is −5 in 8 bits (and 251 if read as unsigned).

What is the largest number in 8, 16, 32 and 64 bits?

Unsigned, a w-bit word holds 0 to 2w−12^w - 1: 255 in 8 bits, 65,535 in 16 bits, 4,294,967,295 in 32 bits and 18,446,744,073,709,551,615 in 64 bits. Signed (two’s complement), the range is −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1, such as −128 to 127 in 8 bits and −32,768 to 32,767 in 16 bits. Larger inputs are rejected; pick a wider word.

What is the difference between a logical and an arithmetic right shift?

A logical shift fills the empty top bits with 0. An arithmetic shift copies the sign bit. In 8 bits, −16 = 1111 0000 shifted right by 2 is 1111 1100 = −4 arithmetically, but 0011 1100 = 60 logically.

Why is NOT 5 equal to −6?

NOT flips every bit, and in two’s complement that always gives −x − 1. NOT 0000 0101 = 1111 1010, which is −6 as a signed 8-bit number (and 250 unsigned).

What happens when 127 + 1 overflows in 8 bits?

127 = 0111 1111, and adding 1 gives 1000 0000. Unsigned, that is 128 and fine. Signed, it is −128, the most negative 8-bit value: the result wrapped around. Pick a larger word size, such as 16 bits, to get 128.

Decimal to Binary Converter

Decimal to binary with each division by 2, padding to 8–64 bits, two’s complement and fractions.

Number Theory & Bases

Hex to Decimal Converter

Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.

Number Theory & Bases

Binary to Decimal Converter

Binary to decimal with each bit’s power of 2 and a running total, signed or unsigned, fractions included.

Number Theory & Bases

Mod Calculator

a mod n with steps, negative numbers, power mod and modular inverses.

Number Theory & Bases

LCM and GCF Calculator

LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.

Number Theory & Bases

Scientific Calculator

Type or tap expressions with exact fractions, memory, variables, constants and complex mode.

Tools

Learn more: For hex, octal and binary input, and bitwise AND, OR and XOR in the full calculator, read base-n and bitwise operations.

Sources and further reading

  1. Computer Organization and Design: The Hardware/Software Interface, RISC-V edition, §2.4 Signed and Unsigned Numbers, Morgan Kaufmann (David A. Patterson and John L. Hennessy)
  2. Hacker’s Delight, 2nd edition, Addison-Wesley (Henry S. Warren Jr., 2012)
  3. The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd edition, §4.1 Positional Number Systems, Addison-Wesley (Donald E. Knuth, 1997)

Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?