Hex Calculator
This hexadecimal calculator adds, subtracts, multiplies and divides hex numbers and applies AND, OR, XOR, NOT and bit shifts. Pick a bit width to wrap results the way a register does: at 8 bits, FF + 1 gives 00 with the carry flag set. Column addition shows each carry, and bit rows show logic operations.
Remainder:
Unsigned:
- Carry: clear
- Borrow: clear
- Signed overflow: clear
Working
Add column by column from the right; a column that reaches 16 writes the excess and carries 1
| carry | 1 | 1 | 1 | |
|---|---|---|---|---|
| 9 | F | 8 | ||
| + | 7 | A | 9 | |
| = | 1 | 1 | A | 1 |
8 + 9 = 17, write 1 carry 1
F + A + 1 = 26, write A carry 1
9 + 7 + 1 = 17, write 1 carry 1
How to add hex numbers on paper
Hex addition is school addition with a bigger threshold. You carry when a column reaches sixteen instead of ten, and digits above nine are letters, so the only new skill is adding letters quickly. Convert them in your head (B + 7 is 11 + 7 = 18 = 16 + 2), write the 2 and carry 1.
- Line the two hex numbers up on the right, padding the shorter one with zeros.
- Add the rightmost column; if the sum is 16 or more, write the sum minus 16 and carry 1.
- Move one column left, add both digits and the carry, and repeat.
- If a carry is left after the last column, write it in front.
The working under the calculator writes each carry above the column it lands in, the same way you would on paper, so you can check homework one column at a time.
Subtraction and borrowing
When the top digit is smaller than the bottom one, borrow from the next column to the left. In hex a borrow is worth sixteen, so A − C becomes (16 + 10) − 12 = 14, which is E. The column you borrowed from is one less when its turn comes.
If the second number is bigger, subtract the other way and put a minus sign in front. With a bit width chosen, the calculator instead wraps around the way hardware does and sets the borrow flag.
Two borrows, in the ones and the sixteens columns
C52 − 3E7 = 86B
Subtract column by column; a column that needs more borrows 16 from its left neighbor
| borrow | 1 | 1 | |
|---|---|---|---|
| C | 5 | 2 | |
| − | 3 | E | 7 |
| = | 8 | 6 | B |
AND, OR and XOR, bit by bit
Bitwise operators work on each bit position separately, with no carries between them, which is why their results are easiest to read in binary. AND keeps a bit only where both inputs have it, so it is used to mask fields out of a value; OR sets bits; XOR flips the bits where the second operand has a 1.
For example, F0 AND 3C is 30, F0 OR 3C is FC and F0 XOR 3C is CC. XOR with the same value twice gives back the original, a property behind simple checksums and the classic swap trick.
| A | B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Carry versus overflow at a fixed width
A processor adds the bits once and reports two separate verdicts. The carry flag says the unsigned result wrapped past the top of the register. The overflow flag says the signed result has the wrong sign, which happens only when two numbers of the same sign produce one of the other sign.
Pick 8, 16, 32 or 64 bits and the calculator reports both, along with borrow for subtraction. The table shows why they are independent: each combination of set and clear occurs at 8 bits. Division at a width is signed as well, so a pattern with the top bit set divides as a negative number; the unsigned quotient is printed under it.
| Sum | Result | Carry | Overflow | Why |
|---|---|---|---|---|
| 7F + 01 | 80 | clear | set | 127 + 1 does not fit in a signed byte |
| FF + 01 | 00 | set | clear | −1 + 1 = 0 is fine signed; unsigned 255 + 1 wraps |
| 80 + 80 | 00 | set | set | both readings are out of range |
| 10 + 20 | 30 | clear | clear | small numbers, no wrap at all |
Shifts
Shifting left by one doubles a number, and by four multiplies it by sixteen, which in hex simply appends a zero: 3A << 4 is 3A0. Shifting right divides and drops the bits that fall off the end. The shift count is typed in decimal, since it counts bit positions, not a value.
For negative numbers the two right shifts differ. The arithmetic shift keeps the sign by copying the top bit in; the logical shift fills with zeros and so needs a width to know where the top is.
Questions people ask
How do you add hex numbers?
Add column by column from the right, as in decimal. When a column reaches 16 or more, write the sum minus 16 and carry 1. For example, 9 + 8 = 17, so you write 1 and carry 1; F + 1 = 16, so you write 0 and carry 1.
What happens when FF + 1 overflows at 8 bits?
The true sum is 100, which needs 9 bits, so an 8-bit register keeps 00 and sets the carry flag. Read as signed numbers, FF is −1 and −1 + 1 = 0, so there is no signed overflow.
How does borrowing work in hex subtraction?
When the top digit is smaller, borrow 16 from the next column. In 30 − 1 the ones column becomes 16 − 1 = F and the 3 drops to 2, giving 2F.
What is the difference between the >> and >>> shifts?
Both move bits to the right. The arithmetic shift >> copies the sign bit into the freed places, so a negative number stays negative; the logical shift >>> fills them with zeros. At 8 bits, F0 >> 4 is FF, while F0 >>> 4 is 0F.
Is division at a bit width signed or unsigned?
Signed. With a width chosen, both operands are read as two’s complement, the way C divides int values, so 80 ÷ 2 at 8 bits is −128 ÷ 2 = −64, which is C0. The line under the result also gives the unsigned reading: 128 ÷ 2 = 64, hex 40.
Why does NOT need a bit width?
NOT flips every bit, and a number has endless leading zeros, so without a width the result would never end. At 8 bits NOT 0F is F0; at 16 bits NOT 000F is FFF0.
Related conversions
Last updated