Hex Calculator

Do arithmetic in base 16 with the carries shown, convert between hex, decimal and binary, and work out how signed integers are stored as bit patterns.

Your results

–

Save this calculation and come back to it any time. Members only — $5 every 3 months, ad-free.

Become a member

How to use the hex calculator

  1. Choose a mode: hex arithmetic, convert, or two's complement.
  2. For arithmetic, enter hex numbers A and B (digits 0–9 and letters A–F, upper or lower case, with or without 0x) and pick an operation.
  3. To convert, say whether your number is in hex, decimal or binary, then type it.
  4. For two's complement, choose the direction and the bit width. Turn a signed decimal such as -42 into its hex pattern, or read a hex pattern such as FF as a signed number.
  5. Long results are split into groups of four hex digits. Each hex digit is exactly four bits.

How hexadecimal works

hex hₙ…h₁h₀ = hₙ×16ⁿ + … + h₁×16¹ + h₀×16⁰, A=10, B=11, C=12, D=13, E=14, F=15 Two's complement of −n in w bits = 2ʷ − n

Hexadecimal is base 16, so each place is worth 16 times the one to its right: 1, 16, 256, 4,096 and so on. Because 16 = 2⁴, one hex digit always stands for exactly four bits. That is why programmers write memory addresses, colors (#1A3FFF) and byte values in hex: it is binary, compressed four to one.

Adding in hex works like adding in decimal, except that you carry when a column reaches 16 instead of 10. In two's complement, a computer stores a negative number −n in w bits as the pattern 2ʷ − n. The leftmost bit then shows the sign: 1 for negative and 0 for positive or zero.

Worked example

The calculator opens with 1A3F + FF:

  1. Column 1 (rightmost): F + F = 15 + 15 = 30 = 16 + 14. Write E (14), carry 1.
  2. Column 2: 3 + F + carry 1 = 3 + 15 + 1 = 19 = 16 + 3. Write 3, carry 1.
  3. Column 3: A + carry 1 = 10 + 1 = 11. Write B.
  4. Column 4: 1. Write 1.

The answer is 1B3E. Check in decimal: 1A3F is 1 × 4,096 + 10 × 256 + 3 × 16 + 15 = 6,719, FF is 255, and 6,719 + 255 = 6,974, which is 1B3E. Multiplying instead gives 6,719 × 255 = 1,713,345 = 1A24C1.

In two's complement mode, the default turns -42 into an 8-bit pattern. 42 is 0010 1010. Flip every bit to get 1101 0101, then add 1 to get 1101 0110, which is 0xD6. The shortcut gives the same answer: 2⁸ − 42 = 214 = 0xD6.

Signed ranges by bit width

Width Smallest Largest -1 in hex
8 bits -128 127 FF
16 bits -32,768 32,767 FFFF
32 bits -2,147,483,648 2,147,483,647 FFFF FFFF
64 bits -9,223,372,036,854,775,808 9,223,372,036,854,775,807 FFFF FFFF FFFF FFFF

A pattern only has meaning once you know its width and whether it is signed. 0xFF is 255 as an unsigned byte but -1 as a signed byte, and the same bits inside a 16-bit number (0x00FF) are simply 255.

Frequently asked questions

How do I convert hex to decimal?

Multiply each digit by its power of 16 and add the results, reading the letters A–F as 10–15. 2F is 2 × 16 + 15 = 47, and 1A3F is 4,096 + 2,560 + 48 + 15 = 6,719. Going the other way, divide by 16 repeatedly and read the remainders from the bottom up.

Why do programmers use hex instead of binary?

It is much shorter and converts to binary instantly, one digit to four bits. A 32-bit value takes 32 binary digits but only 8 hex digits, so 1111 1111 0000 1010 is simply FF0A. Decimal doesn't line up with bits at all, which makes it awkward for masks and memory addresses.

Why is the two's complement of a negative number so large?

A negative number in w bits is stored as 2ʷ minus its size, so -1 becomes the largest possible pattern: all ones, FF in 8 bits. Read as unsigned, that pattern is 255. Read as signed, it is -1. Two's complement is used because addition then works the same way for positive and negative numbers.

What does the 0x in front of a hex number mean?

It is a prefix that marks the number as hexadecimal, so 0x10 means sixteen rather than ten. Other notations include a trailing h (10h) or a # for colors (#FF0000). The calculator accepts input with or without 0x.

Related calculators

  • Binary Calculator — Add, subtract, multiply and divide binary numbers, run AND, OR, XOR and NOT, and convert binary, octal, decimal and hex.
  • Big Number Calculator — Exact arithmetic on whole numbers with hundreds or thousands of digits: add, multiply, divide, powers, factorials and GCD.
  • IP Subnet Calculator — Network and broadcast address, usable host range, masks and binary view for any IPv4 or IPv6 subnet.
  • Scientific Calculator — Free online scientific calculator with trig in degrees or radians, logs, powers, roots, factorials, memory and history.

Results are calculated in your browser. Double-check anything important before you rely on it. See our disclaimer.