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
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
| Row | Carry outOut | Digit 3 | Digit 2 | Digit 1 | Digit 0 |
|---|---|---|---|---|---|
| Carry in | 0 | 1 | 1 | 1 | 0 |
| A | 2 | F | 9 | C | |
| B | 7 | B | 6 | 5 | |
| Result | A | B | 0 | 1 |
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.
- Column 0 (rightmost): C + 5 = 17 (11 in hex), so write 1 and carry 1.
- Column 1: 9 + 6 + 1 = 16 (10 in hex), so write 0 and carry 1.
- Column 2: F + B + 1 = 27 (1B in hex), so write B and carry 1.
- Column 3: 2 + 7 + 1 = 10 (A in hex), so write A.
- 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 | A | B | Result |
|---|---|---|---|
| Bits 19–16 | 0000 | 0000 | 0000 |
| Bits 15–12 | 0010 | 0111 | 1010 |
| Bits 11–8 | 1111 | 1011 | 1011 |
| Bits 7–4 | 1001 | 0110 | 0000 |
| Bits 3–0 | 1100 | 0101 | 0001 |
Hex digits and their values
| Hex digit | Decimal | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
Worked examples
| Calculation | Hex result | Decimal result |
|---|---|---|
| 2F9C + 7B65 | AB 01 | 43,777 |
| 7B65 − 2F9C | 4B C9 | 19,401 |
| 2F9C − 7B65 | -4B C9 | -19,401 |
| FF × FF | FE 01 | 65,025 |
| FFFF FFFF + 1 | 1 00 00 00 00 | 4,294,967,296 |
| F0F0 AND 0FF0 | F0 | 240 |
| F0F0 OR 0FF0 | FF F0 | 65,520 |
| F0F0 XOR 0FF0 | FF 00 | 65,280 |
| 1 << 8 | 1 00 | 256 |
Range of each word size
| Word size | Hex digits | Largest unsigned, in hex | Largest signed, in hex | Most negative, in hex | Largest unsigned, in decimal |
|---|---|---|---|---|---|
| 8-bit | 2 | FF | 7F | 80 | 255 |
| 16-bit | 4 | FF FF | 7F FF | 80 00 | 65,535 |
| 32-bit | 8 | FF FF FF FF | 7F FF FF FF | 80 00 00 00 | 4,294,967,295 |
| 64-bit | 16 | FF FF FF FF FF FF FF FF | 7F FF FF FF FF FF FF FF | 80 00 00 00 00 00 00 00 | 18,446,744,073,709,551,615 |
When a fixed-width result overflows
| Calculation | Word | Result in hex | Unsigned | Signed | Unsigned overflow | Signed overflow |
|---|---|---|---|---|---|---|
| FF + 1 | 8-bit | 00 | 0 | 0 | Yes | No |
| 7F + 1 | 8-bit | 80 | 128 | -128 | No | Yes |
| 80 − 1 | 8-bit | 7F | 127 | 127 | No | Yes |
| 0 − 1 | 8-bit | FF | 255 | -1 | Yes | No |
| FFFF + 1 | 16-bit | 00 00 | 0 | 0 | Yes | No |
| 7FFF + 1 | 16-bit | 80 00 | 32,768 | -32,768 | No | Yes |
| 7FFFFFFF + 1 | 32-bit | 80 00 00 00 | 2,147,483,648 | -2,147,483,648 | No | Yes |
Bitwise results for F0F0 and 0FF0
| Operation | Result in hex | Result in binary |
|---|---|---|
| AND | 00 F0 | 0000 0000 1111 0000 |
| OR | FF F0 | 1111 1111 1111 0000 |
| XOR | FF 00 | 1111 1111 0000 0000 |
How to add and subtract hex numbers
- 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.
- 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.
- 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.
- 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.
- 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
- 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
- Hexadecimal, Wikipedia
- Two’s complement, Wikipedia
- Bitwise operations in JavaScript, MDN Web Docs
More free tools
- Binary CalculatorAdd, subtract, multiply, divide, AND, OR, XOR, NOT and shift binary, hex, octal or decimal numbers, with overflow flags and two’s complement.
- Number Base ConverterConvert whole numbers between binary, octal, decimal, hex and any base from 2 to 36, up to 5,000 digits, with grouped digits and a two’s-complement view.
- 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.
- 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.
- IP Address ConverterConvert an IPv4 address to a decimal integer, hex or binary and back, expand or compress IPv6, and see its reverse DNS name and whether it is private.
- String Escape / UnescapeEscape or unescape text as a JSON, JavaScript, Java, Python, CSV, SQL, regex, shell or XML string, with a count of escapes and the position of any bad one.
More: Encoding and number systemsAll developer toolsAll toolsHome & DIY calculators