Skip to main content
ponderloop

Hex Calculator: Add, Subtract, Multiply, Divide and Bitwise

2F9C + 7B65 in hex is AB01, which is 43,777 in decimal (12,188 + 31,589). Hex addition works like decimal addition, column by column from the right, except a column carries when its total reaches 16 instead of 10: C + 5 = 17, which is 11 in hex, so write 1 and carry 1. Held in a 16-bit word the same bits read as -21,759 signed, so the signed result overflows even though the unsigned one fits. The calculator below also subtracts, multiplies, divides with a remainder, applies AND, OR, XOR, NOT and shifts, on hex numbers of any size or in an 8, 16, 32 or 64-bit word, and shows each answer in hex, decimal, binary and octal.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

Type hex digits 0 to 9 and A to F, in any case. Spaces, underscores, a minus sign and a 0x prefix are fine. Whole numbers only; a shift count is a plain decimal number. With a fixed word size, every result shows both readings of its bits, signed and unsigned.

Result

Result: AB01 hex = 43,777 in decimal.

Unlimited word: exact integers of any size, no wrap-around. A negative number counts as an endless run of 1 bits in front.

Binary (base 2)
1010 1011 0000 0001
Octal (base 8)
125 401
Decimal (base 10)
43,777
Hexadecimal (base 16)
AB 01

Long-hand working

Long addition in hexadecimal, one column per hex digit, most significant digit on the left
RowCarry outOutDigit 3Digit 2Digit 1Digit 0
Carry in01110
A2F9C
B7B65
ResultAB01

Each column adds the two digits and the carry into it; a total of 16 or more writes the total minus 16 and carries 1 to the column on the left. Exact sum: AB01.

  1. Column 0 (rightmost): C + 5 = 17 (11 in hex), so write 1 and carry 1.
  2. Column 1: 9 + 6 + 1 = 16 (10 in hex), so write 0 and carry 1.
  3. Column 2: F + B + 1 = 27 (1B in hex), so write B and carry 1.
  4. Column 3: 2 + 7 + 1 = 10 (A in hex), so write A.
  5. No carry out of the last column.

Bit grid

Unlimited size, drawn as 20-bit two’s complement: wide enough for the numbers and the result.

Bits of each value, four at a time
BitsABResult
Bits 19–16000000000000
Bits 15–12001001111010
Bits 11–8111110111011
Bits 7–4100101100000
Bits 3–0110001010001

Hex digits and their values

Each hex digit stands for four bits (a nibble). Two hex digits make one byte.
Hex digitDecimalBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111

Worked examples

Unlimited word size, so nothing wraps. Each result is computed by the calculator.
CalculationHex resultDecimal result
2F9C + 7B65AB 0143,777
7B65 − 2F9C4B C919,401
2F9C − 7B65-4B C9-19,401
FF × FFFE 0165,025
FFFF FFFF + 11 00 00 00 004,294,967,296
F0F0 AND 0FF0F0240
F0F0 OR 0FF0FF F065,520
F0F0 XOR 0FF0FF 0065,280
1 << 81 00256

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). A signed value with the top hex digit 8 to F is negative.
Word sizeHex digitsLargest unsigned, in hexLargest signed, in hexMost negative, in hexLargest unsigned, in decimal
8-bit2FF7F80255
16-bit4FF FF7F FF80 0065,535
32-bit8FF FF FF FF7F FF FF FF80 00 00 004,294,967,295
64-bit16FF FF FF FF FF FF FF FF7F FF FF FF FF FF FF FF80 00 00 00 00 00 00 0018,446,744,073,709,551,615

When a fixed-width result overflows

Unsigned overflow (a carry or borrow) and signed overflow are separate tests on the same bits: a result can overflow one reading and not the other.
CalculationWordResult in hexUnsignedSignedUnsigned overflowSigned overflow
FF + 18-bit0000YesNo
7F + 18-bit80128-128NoYes
80 − 18-bit7F127127NoYes
0 − 18-bitFF255-1YesNo
FFFF + 116-bit00 0000YesNo
7FFF + 116-bit80 0032,768-32,768NoYes
7FFFFFFF + 132-bit80 00 00 002,147,483,648-2,147,483,648NoYes

Bitwise results for F0F0 and 0FF0

Each hex digit is compared with the digit in the same place, bit by bit. Shown as 16-bit values.
OperationResult in hexResult in binary
AND00 F00000 0000 1111 0000
ORFF F01111 1111 1111 0000
XORFF 001111 1111 0000 0000

How to add and subtract hex numbers

  1. Line up the digits. Write the numbers one above the other, aligned on the right, and treat the letters as digits: A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15. For 2F9C + 7B65 the rightmost column is C + 5.
  2. Add column by column and carry at 16. Add the two digits and any carry, from the right. If the total is 16 or more, write the total minus 16 and carry 1 to the next column. Here C + 5 = 17, so write 1 and carry 1; 9 + 6 + 1 = 16, so write 0 and carry 1; F + B + 1 = 27, so write B and carry 1; 2 + 7 + 1 = 10, so write A. The answer is AB01.
  3. Subtract and borrow 16. Subtract column by column from the right. When the top digit is smaller than the digit below it plus any borrow, borrow 1 from the next column, which adds 16 to this one. 7B65 − 2F9C is 4B C9. When A is smaller than B, work out B − A and put a minus sign in front: 2F9C − 7B65 is −4B C9.
  4. Multiply, divide and compare bits. Multiplication and division give the same answers as in decimal, written in hex: FF × FF is FE 01, and 3E8 ÷ 7 is 8E remainder 6 (the quotient is cut toward zero). AND, OR and XOR compare the two numbers bit by bit: F0F0 AND 0FF0 is F0 and F0F0 XOR 0FF0 is FF 00.
  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ⁿ, and the tool says whether the unsigned result overflowed (a carry or borrow) or the signed one did. Choose Unlimited for exact arithmetic of any size with no wrap-around.

Frequently asked questions

How do I add two hex numbers?
Add them column by column from the right, treating A to F as 10 to 15, and carry 1 whenever a column totals 16 or more. 2F9C + 7B65 gives AB01, which is 43,777 in decimal. The long-hand table under the calculator shows the carry into every column and the step for each one.
How do I subtract hex numbers?
Subtract column by column from the right, borrowing 16 from the next column whenever the top digit is too small. 7B65 − 2F9C is 4B C9. If the first number is smaller, the result is negative: 2F9C − 7B65 is −4B C9, and the long-hand table works out B − A for you. In a fixed word size the negative result is stored in two’s complement instead: 3 − 5 in 8 bits is FE, which reads as -2 signed.
What is the largest number a hex digit string can hold?
There is no limit in Unlimited mode: the calculator uses exact big integers, so long hex strings such as 128-bit or 256-bit values work, and FFFF FFFF + 1 is 1 00 00 00 00. In a fixed word the largest unsigned value is all F digits: FF in 8 bits, FFFF in 16, FFFF FFFF in 32 and FF FF FF FF FF FF FF FF in 64.
What is two’s complement in hex?
Two’s complement is how computers store negative whole numbers in a fixed number of bits: −x is stored as 2ⁿ − x, so in hex you subtract x from 100 (8 bits), 10000 (16 bits) and so on. In 8 bits, −5 is FB, and in general a hex number whose leading digit is 8 to F is negative when read as signed. The same adder works for signed and unsigned numbers; only the reading differs. 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: FF + 1 in 8 bits carries out and wraps to 0. A signed overflow means the signed result did not fit: 7F + 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).
How does hex division work, and what about the remainder?
The calculator gives an integer quotient and a remainder, both in hex, with the quotient cut toward zero and the remainder taking the sign of A, as in C, Java and JavaScript: 3E8 ÷ 7 is 8E remainder 6 (1,000 ÷ 7 = 142 remainder 6 in decimal). In a fixed word you choose whether the bits are read as signed or unsigned, because FF is −1 signed but 255 unsigned. Dividing by 0 is reported as an error.
How do I convert a hex result to decimal or binary?
You do not need to: every result is shown in hex, decimal, binary and octal at once, with a Copy button for each. By hand, each hex digit is four bits (1010 for A, 0101 for 5), so AB01 is 1010 1011 0000 0001 in binary. For decimal, multiply each digit by a power of 16. To convert a single number rather than calculate with it, use the number base converter.
How are the shifts and bitwise operators defined?
AND is 1 only where both bits are 1, OR where either is, XOR where they differ, and NOT flips every bit of the word. A left shift by n multiplies by 2ⁿ (1 << 8 is 1 00); a logical right shift fills the top with 0 and an arithmetic right shift copies the sign bit. The shift count is a plain decimal number. For NAND, NOR and bit-by-bit grids in binary, see the binary calculator.

How it works

Each number is read digit by digit in base 16 into an exact whole number (JavaScript BigInt), using the same parser as the number base converter, and every calculation is done by the binary calculator’s routines on that value; only the hex operand reading, the hex-first summary and the long-hand working are specific to this page. With a fixed word size n, an operand 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 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 truncates toward zero and reads the patterns as signed or unsigned as you choose. Shifts by more than the word size are refused. With Unlimited size nothing wraps, and negative values follow the endless two’s complement convention of BigInt. The long-hand table is drawn for addition and subtraction of non-negative numbers of up to 16 hex digits: each column adds (or subtracts) the two digits and the carry (or borrow) into it, writes the total modulo 16 and passes a carry (or borrow) of 1 when the total reaches 16 (or goes below 0). Its answer is the exact one; in a fixed word the result rows keep only the low n bits.

Sources

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