StringMash.com

Hex calculator

Hexadecimal sums with the carries and borrows written out, and the answer in decimal and binary too.

9 characters
Updates as you type
Hex1B3E
Decimal6974
Binary1 1011 0011 1110

Show the steps
  1. Add column by column from the right. When a column reaches 16, write what's left and carry 1.
  2. carries 1 1
  3. 1 A 3 F
  4. + F F
  5. ---------
  6. 1 B 3 E

Using the calculator

Type a sum the way you'd write it: `1A3F + FF`, `100 - 1`, `FF * 2`, `FF / 10`. The answer comes back in hex, decimal and binary, each with a copy button, and the working shows how it was done.

Besides + − × and ÷, it understands `mod`, `AND`, `OR`, `XOR`, `NOT`, the shifts `<<`, `>>` and `>>>`, and `ROL` and `ROR` for rotating. Letters can be either case, and `0x1F` or `1Fh` both mean hex 1F. To mix in a decimal or binary number, change Numbers are in.

Hex addition

It works exactly like decimal addition, except that a column carries when it reaches 16, not 10. In `1A3F + FF`, F + F is 30 in decimal, which is 1E in hex, so write E and carry 1. Then 3 + F + 1 is 19, which is 13 in hex, so write 3 and carry 1. A + 1 is B, and the 1 at the front comes down. The answer is 1B3E.

The working writes the carries above the columns, so you can check each one. It helps to know the digits past 9 by heart. A is 10, B 11, C 12, D 13, E 14 and F 15.

Subtraction, multiplication and division

Subtraction borrows 16 from the next column when the top digit is too small. In `100 − 1` the first two columns both have to borrow, which is why the answer is FF, and the working marks each borrow with a b. If the second number is bigger, the answer is negative, and the calculator also gives its two's complement, which is how a computer stores it.

Multiplication and division are worked through decimal, which is how most people check them by hand. Division is whole-number division, as in programming languages, with the remainder given separately: `FF / 10` is F remainder F.

0xCAFEBABE

Programmers like hex numbers that spell words. Every Java class file starts with the four bytes CA FE BA BE, chosen as a nod to the baristas at Peet's Coffee & Tea. 0xDEADBEEF was used on IBM RS/6000 systems and on PowerPC Macs to mark memory that hadn't been set yet, so a crash dump full of it was easy to spot. Both work because hex digits include six letters, and words built from A to F are rare enough to stand out.

Questions

How do you add hex numbers?

Column by column from the right, as in decimal, but carry when a column reaches 16. F + 1 is 10 in hex.

How do I enter a negative hex number?

Put a minus sign in front, like -FF. The answer to a subtraction that goes below zero comes back negative, with its 32-bit two's complement shown too.

Does it do decimal points?

No. It works with whole numbers, as most programming does with hex.

How big can the numbers be?

As big as you like. It uses arbitrary-length integers, so 64-bit and longer values are exact.

Sources

Added . What's new