Hex calculator
Add, subtract, multiply and divide hexadecimal numbers, with the carries, borrows, partial products and long division laid out column by column. Bitwise AND, OR, XOR, NOT and shifts too, at any width.
Working
| carries | 1 | 1 | 1 | ||
|---|---|---|---|---|---|
| first number | 3 | A | F | 7 | |
| second number | 1 | C | 9 | D | |
| sum | 5 | 7 | 9 | 4 |
1 carried into the column below it
- Column 1 (16⁰): 7 + D (13) = 20, which is 14 in hex. Write 4, carry 1.
- Column 2 (16¹): F (15) + 9 + 1 carried = 25, which is 19 in hex. Write 9, carry 1.
- Column 3 (16²): A (10) + C (12) + 1 carried = 23, which is 17 in hex. Write 7, carry 1.
- Column 4 (16³): 3 + 1 + 1 carried = 5. Write 5.
Adding hex: carry at 16
Hex adds like decimal, except a column only carries when it reaches sixteen. The easy way to add two hex digits is through decimal: B + 7 is 11 + 7 = 18, and 18 is one 16 and 2 more, so write 2 and carry 1. Here is 2B7 + 5E9, which is 695 + 1513 = 2208 in decimal:
| carries | 1 | 1 | ||
|---|---|---|---|---|
| first number | 2 | B | 7 | |
| second number | 5 | E | 9 | |
| sum | 8 | A | 0 |
- Column 1 (16⁰): 7 + 9 = 16, which is 10 in hex. Write 0, carry 1.
- Column 2 (16¹): B (11) + E (14) + 1 carried = 26, which is 1A in hex. Write A, carry 1.
- Column 3 (16²): 2 + 5 + 1 carried = 8. Write 8.
The hex addition table
Every sum of two hex digits. Two digit answers carry: the 1 in front is the carry into the next column.
| + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
| 2 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 |
| 3 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 |
| 4 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 |
| 5 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 |
| 6 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 |
| 7 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 8 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 9 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| A | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| B | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A |
| C | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B |
| D | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C |
| E | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D |
| F | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E |
Subtracting hex: borrow 16
When a column's top digit is smaller than the one below, borrow from the next column to the left. The borrow is worth 16 in this column, and the column that lent is one less. Here is 4A2 − 1B7, or 1186 − 439 = 747:
| borrows | 1 | 1 | |
|---|---|---|---|
| first number | 4 | A | 2 |
| second number | 1 | B | 7 |
| difference | 2 | E | B |
1 marks a column that lent 16 to the column on its right.
- Column 1 (16⁰): 2 − 7 is below zero, so borrow 16 from the next column: 11, which is B. Write B.
- Column 2 (16¹): A (10) − 1 (lent) − B (11) is below zero, so borrow 16 from the next column: 14, which is E. Write E.
- Column 3 (16²): 4 − 1 (lent) − 1 = 2. Write 2.
A result below zero is shown with a minus sign. In a fixed width it wraps round instead, and the digits left are the two's complement of the negative answer: 10 − 20 in 32 bits is FFFFFFF0, which is −16 in decimal.
Multiplying hex
Long multiplication works as in decimal: one row per digit of the second number, each shifted one place further left, then add the rows. Here is 2F × 1B, or 47 × 27 = 1269:
| first number | 2 | F | |
|---|---|---|---|
| second number | 1 | B | |
| 2F × B, shifted 0 | 2 | 0 | 5 |
| 2F × 1, shifted 1 | 2 | F | 0 |
| product | 4 | F | 5 |
- Digit 1 of the multiplier is B (11): 2F × B = 205 (47 × 11 = 517 in decimal).
- Digit 2 of the multiplier is 1: 2F × 1 = 2F (47 × 1 = 47 in decimal), shifted 1 place left.
- Add the rows to get 4F5.
Dividing hex
Long division brings down one hex digit at a time and asks how many times the divisor fits, which is a number from 0 to F. Here is FACE ÷ 1B, or 64206 ÷ 27 = 2378 remainder 0:
- Bring down F: F is smaller than 1B, so the quotient digit is 0.
- Bring down A: 1B goes into FA 9 times (250 ÷ 27 in decimal). Write 9, subtract F3, leaving 7.
- Bring down C: 1B goes into 7C 4 times (124 ÷ 27 in decimal). Write 4, subtract 6C, leaving 10.
- Bring down E: 1B goes into 10E 10 times (270 ÷ 27 in decimal). Write A, subtract 10E, leaving 0.
Quotient 94A, remainder 0. To check a step by hand, convert to decimal with the hex to decimal converter.
Hex arithmetic is binary arithmetic
A hex digit is four bits, so a hex sum is a binary sum read four bits at a time, and a hex carry is the carry out of the top bit of a nibble. That is why hex is the notation for registers, memory addresses and masks: the binary calculator gives the same bits, and the hex to binary converter shows how each digit maps to its four bits.
Questions
How do you add hex numbers?
Line them up on the right and add column by column. Convert letters to their values (A = 10 up to F = 15), add, and if the column total is 16 or more, subtract 16, write that digit and carry 1. B + 7 is 11 + 7 = 18, which is 16 + 2, so write 2 and carry 1.
How do you subtract hex numbers?
Column by column from the right. When the top digit is smaller, borrow 16 from the column to the left: 2 − 7 becomes 16 + 2 − 7 = 11, which is B, and the next column along is one less. If the second number is bigger, subtract the other way round and put a minus sign on the answer.
What is FF + 1 in hex?
FF + 1 = 100. F + 1 is 16, which writes 0 and carries 1; the next F plus that carry is 16 again, and the final carry becomes a new digit. It is the same as 255 + 1 = 256, and in an 8 bit register it wraps to 00, which is overflow.
How do you multiply hex numbers?
Multiply the first number by each digit of the second, as in decimal long multiplication, shifting each row one place left, then add the rows. For single digit products it helps to go through decimal: B × 7 = 11 × 7 = 77 = 4 × 16 + 13, which is 4D. The calculator shows each row with its decimal check.
Is hex arithmetic the same as binary arithmetic?
Yes. One hex digit is four bits, so adding hex is adding binary four bits at a time, and a hex carry is the carry out of the top bit of each nibble. The answers are the same bits written more compactly, which is why the bitwise operations here write the hex out in binary.
How long can the numbers be?
Up to 64 hex digits (256 bits) each, worked exactly with no rounding. Choose a width of 8, 16, 32 or 64 bits to see what a register of that size would do: results wrap round, and the calculator flags overflow.