Hex Calculator

Hex calculator: add, subtract, multiply and divide hexadecimal numbers, or apply AND, OR, XOR and shifts. Results come in hex, decimal and binary, and additions and subtractions show the carries column by column. Values of any size are exact.

Hexadecimal arithmetic
Result
Hex1A6A
Decimal6,762
Binary1101001101010
Values are exact for any length · a minus sign is accepted · your input stays in this browser
Column addition, right to left
ColumnDigit ADigit BCarry inSumWriteCarry out
1FB026A1
2321660
3A0010A0
4100110

Hex addition: columns, carries and the sixteen rule

Adding hexadecimal numbers works like decimal addition on paper. Line the numbers up on the right, add each column from the right, write the last digit of the column total and carry the rest to the next column. The only change is the size of a column: it holds 16 values, so a total of 16 or more carries. Letters are worth 10 to 15 while you add, and the total is written back as a hex digit.

The example is 1A3F + 2B. In the first column F + B = 15 + 11 = 26, which is 1 sixteen and 10 left over, so write A and carry 1. In the second column 3 + 2 + 1 = 6, with no carry. The next two columns have only the first number, so 1A is copied down. The sum is 1A6A, and the diagram shows each step.

Adding 1A3F and 2B column by columncolumn 1: F + B + carry 0 = 26, write A, carry 1; column 2: 3 + 2 + carry 1 = 6, write 6, carry 0; column 3: A + 0 + carry 0 = 10, write A, carry 0; column 4: 1 + 0 + carry 0 = 1, write 1, carry 0.carry1F3A1B2+A6A1right to left: column totals 26, 6, 10, 1
1A3F + 2B = 1A6A. A column total of 16 or more writes (total − 16) and carries 1 to the next column, exactly as in decimal with 10.

The calculator above prints this column table under every sum, with the carry in and out of each column, so that a result can be checked against a hand calculation. A hex addition table helps until the sums become automatic. Read it as with a multiplication table: the row digit plus the column digit gives the entry.

Hexadecimal addition table (row + column)
+0123456789ABCDEF
00123456789ABCDEF
1123456789ABCDEF10
223456789ABCDEF1011
33456789ABCDEF101112
4456789ABCDEF10111213
556789ABCDEF1011121314
66789ABCDEF101112131415
7789ABCDEF10111213141516
889ABCDEF1011121314151617
99ABCDEF101112131415161718
AABCDEF10111213141516171819
BBCDEF101112131415161718191A
CCDEF101112131415161718191A1B
DDEF101112131415161718191A1B1C
EEF101112131415161718191A1B1C1D
FF101112131415161718191A1B1C1D1E

Hex subtraction and borrowing

Subtraction mirrors addition. Subtract column by column from the right. When the top digit is smaller than the bottom one, borrow from the next column to the left: that column loses 1 and this one gains 16. Take 1A3F − 2B. F − B = 15 − 11 = 4. 3 − 2 = 1. The upper two digits come down, giving 1A14.

Borrowing chains show up when a zero sits in the way. 100 − 1 needs the 1 in the third column to give up its value through two zero columns: the first column borrows 16 from the second, which has to borrow from the third. The result is FF, and the borrow table that the calculator prints for this sum shows the chain.

100 − 1 in hex, column by column
ColumnDigit ADigit BBorrow inResultWriteBorrow out
101015F1
200115F1
3101000

Hex multiplication

Multiply as you would in decimal: take the digits of the second number one at a time, multiply the whole first number by each, shift each partial product one column left per position, and add. The single-digit products come from the table below. Seeing that 7 × 6 is 2A is a lookup, not a calculation, and the table is worth printing if you do hex arithmetic by hand.

Hexadecimal multiplication table (row × column)
×0123456789ABCDEF
00000000000000000
10123456789ABCDEF
202468ACE10121416181A1C1E
30369CF1215181B1E2124272A2D
4048C1014181C2024282C3034383C
505AF14191E23282D32373C41464B
606C12181E242A30363C42484E545A
707E151C232A31383F464D545B6269
8081018202830384048505860687078
909121B242D363F48515A636C757E87
A0A141E28323C46505A646E78828C96
B0B16212C37424D58636E79848F9AA5
C0C1824303C4854606C7884909CA8B4
D0D1A2734414E5B6875828F9CA9B6C3
E0E1C2A38465462707E8C9AA8B6C4D2
F0F1E2D3C4B5A69788796A5B4C3D2E1

An example: 1A × F. F × A = 15 × 10 = 150 = 96 in hex (9 sixteens and 6), so write 6 and carry 9. F × 1 = F, and adding the carry 9 gives 15 + 9 = 24, which is 18 in hex, so write 18. The product is 186, which is 390 in decimal and agrees with 26 × 15. Multiplying by 10 in hex shifts left one column, as multiplying by 10 does in decimal, and multiplying by 2 is a shift of one bit.

A full multiplication by hand: 1A × 2B

Take the second number a digit at a time, starting from the right. B is 11 and 1A is 26, so 1A × B = 286, which is 11E in hex (1 × 256 + 1 × 16 + 14). The next digit is 2, and 1A × 2 = 34 in hex. Because the 2 sits one column to the left, its product is shifted one place left, written 340. Add the two partial products: 11E + 340 = 45E. In decimal this is 26 × 43 = 1,118, and 45E is 4 × 256 + 5 × 16 + 14 = 1,118. Every step uses either the table or an addition with carries, and the shift is just a zero appended.

Hex division, quotient and remainder

Division gives two results, a whole quotient and a remainder, and the calculator offers both: the divide operation shows the quotient with the remainder beside it, and mod gives only the remainder. 1A3F ÷ 2B is 9C remainder B: 2B goes into 1A3F 9C times, with B left over. Check by multiplying back: 9C × 2B + B = 1A3F.

Long division works column by column from the left, as in decimal, with the multiples of the divisor taken from the multiplication table. The remainder is always smaller than the divisor, which makes it a quick test: a remainder equal to or above the divisor means the quotient digit was one too small. Dividing by a power of 16 is a shift: dividing by 10 removes the last digit, which is the remainder, and dividing by 100 removes two. That is the cheapest way to split a value into its high and low parts. Division by zero has no result, and the calculator reports it instead of returning a number.

Bitwise AND, OR, XOR and NOT

Bitwise operations combine two numbers one bit position at a time. They are the reason hex is used for flags and masks: each hex digit corresponds to four positions, so the digit-wise result can be read straight from the hex. AND keeps a bit only where both numbers have 1. OR keeps it where either has 1. XOR keeps it where exactly one does. The table works the same pair of values through all three.

F0F0 and FF00 combined bit by bit
OperationHex resultBinary of the resultReading
ANDF0001111 0000 0000 0000Only where both have a 1
ORFFF01111 1111 1111 0000Wherever either has a 1
XOR0FF00000 1111 1111 0000Where exactly one has a 1

The inputs are F0F0 = 1111 0000 1111 0000 and FF00 = 1111 1111 0000 0000. NOT, which flips every bit, depends on the width of the register: NOT 0F is F0 in 8 bits and FFF0 in 16 bits, because the wider number has more bits to flip. Masks use AND to select bits (value AND 0F keeps the low digit), set bits with OR, flip chosen bits with XOR, and clear bits with AND against the NOT of a mask. The hex to binary page lists the masks seen most often.

Shifts: multiplying and dividing by powers of two

Shifting moves every bit a number of places. A left shift by n multiplies by 2^n, and a right shift by n divides by 2^n, discarding the remainder. In hex the shifts by multiples of four are easy to read, since one hex digit is four bits: shifting left by 4 appends a 0 digit (1 becomes 10) and shifting right by 4 drops the last digit (F0 becomes F). Shifting by 1, 2 or 3 bits mixes the digits and is easier to see in binary.

Shifts in hex
OperationResultSame as
1 << 410× 16
1 << 8100× 256
FF << 4FF0× 16, one digit appended
F0 >> 4F÷ 16, one digit dropped
ABC >> 8A÷ 256, two digits dropped
1 << 1F80000000The highest bit of a 32-bit value

Overflow: when the result does not fit the register

The calculator gives exact results of any size. Processors do not: a register holds a fixed number of bits, and a result that needs more wraps around, with the extra high bits discarded. FF + 1 is 100, but in an 8-bit register it becomes 00. The same happens at every width, and for signed integers the wrap also flips the sign.

Largest unsigned value plus one, at three register widths
WidthLargest valuePlus 1, exactPlus 1, wrapped7F…F plus 1, read as signed
8-bitFF10000−128
16-bitFFFF100000000−32768
32-bitFFFFFFFF10000000000000000−2147483648

The last column is the signed case: adding 1 to the largest positive value turns it into the most negative one. 7F + 1 is 80 in 8 bits, which is −128. Many security bugs trace back to this wrap, for example a length check that adds two sizes and compares the wrapped result. If a sum from the calculator looks larger than a field can store, an overflow is the first thing to check.

Negative results and two's complement

Subtracting a larger number from a smaller one gives a negative result. The calculator shows it with a minus sign, for example 1 − 2 = −1. A processor stores the same result as a two's complement pattern, 2^n − 1 in n bits, so 1 − 2 is FFFFFFFF in a 32-bit register. The relation is mechanical: add 2^n to the negative value, or invert all bits and add 1. For the stored form at an exact width, use the decimal to hex converter, which shows it for a negative input.

Address arithmetic: offsets, array elements and buffer ends

The most common reason to add hex numbers is address arithmetic. A buffer that starts at 0x7FFE0000 and has a field at offset 0x1A40 has that field at 0x7FFE1A40: the offset adds to the low digits and nothing carries. An array of 12-byte records that starts at 0x4000 has its element 0x0A at 0x4000 + 0xA × 0xC = 0x4000 + 0x78 = 0x4078. The last byte of a buffer is its start plus its length minus one, so a 0x100-byte buffer at 0x8000 ends at 0x80FF, not 0x8100.

The difference between two pointers is a subtraction. If a crash report gives the faulting address 0x00402F1C and the module loads at 0x00400000, the offset into the module is 0x2F1C, which is what a map file or debugger symbol table lists. Subtracting the base addresses first removes the load address so that offsets in the file and in memory can be compared.

Subtracting by adding: two's complement arithmetic

Processors have no separate subtraction circuit for signed numbers; they add the two's complement. To compute 50 − 20 in 8 bits, take the complement of 20: invert the bits (20 becomes DF) and add 1, giving E0. Then 50 + E0 = 130, and the 8-bit register keeps the low byte, 30. The carry out of the top (the 1 in 130) is discarded, and the result 30 is the correct difference.

You can do the same on paper when a borrow chain looks messy: take the complement of the number to subtract, add, and drop the carry. The trick is also how to see that a negative result is a large unsigned number. 20 − 50 in 8 bits is D0, which as an unsigned byte is 208 and as a signed byte is −48. The calculator gives −30 in hex (that is, −48), which is the mathematical result.

Checking hex arithmetic

Three checks work without a calculator. First, convert the operands and the result to decimal for small values and verify the sum there. Second, check the last digit: the last digit of a sum is the last digits added, modulo 16. 1A3F + 2B ends in F + B = 26 = 1A, so the sum ends in A. Third, use casting out fifteens, which is the hex counterpart of casting out nines: the digit sum of a hex number is congruent to the number modulo 15. For 1A3F the digits sum to 1 + 10 + 3 + 15 = 29, which leaves 14 after dividing by 15; for 2B the sum is 2 + 11 = 13; the total 14 + 13 = 27 leaves 12, and the result 1A6A has digits that sum to 1 + 10 + 6 + 10 = 27, which leaves 12. The check works for addition, subtraction and multiplication.

Color arithmetic in hex

Hex arithmetic is also how colors are blended and adjusted, channel by channel. To mix two colors equally, average each pair of channels: the red channels FF and 00 give (FF + 00) ÷ 2 = 7F with a remainder, so a 50% mix of white and black is #7F7F7F or #808080 depending on rounding. To lighten a color, add the same amount to every channel and cap each at FF: #0B5E6B plus 20 per channel is #2B7E8B. Adding without the cap overflows, and 0xF0 + 0x20 = 0x110 wraps to 10 in a byte, which turns a light channel dark.

Because each channel is a byte, the byte-width overflow rules above apply to every operation. When adjusting colors, clamp the result between 00 and FF after each step. The conversion of a finished color to decimal channels is on the hex to RGB page.

This calculator compared with programmer calculators

The Windows Calculator in Programmer mode and the macOS Calculator offer the same operations on fixed word sizes: 64-bit by default, with a switch to 32, 16 or 8 bits. The size controls how overflow and negative numbers behave. This calculator works on unlimited integers instead, which suits address arithmetic and cryptographic values of 128 to 4,096 bits but does not wrap. To see what a result looks like in a register, apply the width yourself: for 8 bits, keep the last two hex digits; for 32 bits, the last eight.

The other difference is the working. A programmer calculator shows only the answer. This one prints the carry or borrow for each column of an addition or subtraction, which is useful when the point is to check a manual calculation or to see where an error came in. For converting the result to other bases, use the hex converter.

Hex arithmetic in Python, JavaScript and the shell

Python
hex(0x1A3F + 0x2B)               # '0x1a6a'
hex(0x1A3F * 0x2B)               # product
divmod(0x1A3F, 0x2B)             # (156, 11): quotient and remainder in decimal
hex(0xF0F0 ^ 0xFF00)             # '0xff0'
JavaScript
(0x1A3F + 0x2B).toString(16)      // '1a6a'
(0xF0F0 & 0xFF00).toString(16)    // 'f000'
(0x1A3Fn * 0x2Bn).toString(16)    // BigInt: exact for any size
Shell and PowerShell
printf '%X\n' $((0x1A3F + 0x2B))   # 1A6A
printf '%X\n' $((0xF0F0 ^ 0xFF00)) # FF0

"{0:X}" -f (0x1A3F + 0x2B)         # 1A6A

JavaScript's bitwise operators convert their operands to 32-bit signed integers, so 0xFFFFFFFF | 0 is −1 and shifts above 31 wrap. Use BigInt (the n suffix) for 64-bit and wider masks.

Questions about the hex calculator

How do you add hexadecimal numbers?

Add column by column from the right, as in decimal, but carry when a column reaches 16 instead of 10. F + B = 15 + 11 = 26, which is 1A, so write A and carry 1. Letters count as 10 to 15 while you add.

Can the calculator handle negative hex numbers?

Yes. A minus sign in front of a value is accepted, and the result can be negative, shown as −1A rather than in a fixed width. To see the stored two's complement form at a given width, use the decimal to hex converter.

What is FF + 1 in hex?

100. FF is 255, and 255 + 1 = 256, which is 1 × 16² and so 100 in hex. In an 8-bit register the result wraps to 00, and the 1 is lost as a carry.

What is the difference between XOR and OR?

OR gives 1 when either bit is 1, including when both are. XOR gives 1 only when exactly one is. F0F0 OR FF00 is FFF0, and F0F0 XOR FF00 is 0FF0, because the top byte where both have ones turns to zero under XOR.

Does the calculator limit the number of digits?

No. Values are exact for any length, because they are held as arbitrary-precision integers. A fixed width is applied only when you apply a mask or look at overflow, which are operations of an 8, 16, 32 or 64-bit register and not of the numbers themselves.