# What is the answer in hex?

Adds, subtracts, multiplies or divides two hexadecimal (base 16) whole numbers exactly at any size, shows the result in hex, decimal and binary, and converts decimal numbers to hex.

- Page: https://www.acalculator.org/logic/hex-calculator
- JSON spec: https://www.acalculator.org/logic/hex-calculator.json
- Version: d886ba5b7cb3

## Default answer

Example with the default inputs (First hex number 8AB, Operation +, Second hex number B78): 8AB + B78 = 1423 in hex (5,155 in decimal).

## Inputs

| Key | Label | Description |
| --- | --- | --- |
| a | First hex number | The first number in hexadecimal: the digits 0 to 9 and the letters A to F, with an optional minus sign. |
| op | Operation | What to do with the two numbers: add, subtract, multiply, or divide (whole-number division with a remainder). |
| b | Second hex number | The second number in hexadecimal. |
| dec | Decimal number to convert | Any whole decimal number to write in hexadecimal. |

## Outputs

| Key | Label | Description |
| --- | --- | --- |
| hex | Result in hex | The result in hexadecimal, capital letters; for division, the whole-number quotient. |
| remainder | Remainder (hex) | For division: what is left over, A − quotient × B, in hexadecimal. |
| decimal | Result in decimal | The result as a decimal whole number. |
| binary | Result in binary | The result in binary (base 2). |
| aDecimal | First number in decimal | The first hex number in decimal. |
| bDecimal | Second number in decimal | The second hex number in decimal. |
| decHex | Decimal number in hex | The decimal number to convert, written in hexadecimal. |

## Method

Each hex number is read in base 16 (digit × 16ⁿ), the whole numbers are added, subtracted, multiplied or divided exactly, and the result is written back in base 16.

## Assumptions

- Numbers are whole (no hex fractions) and may be negative; a minus sign is written in front, not as two’s complement.
- Division gives the whole-number quotient rounded toward zero and the remainder, A − quotient × B, which has the sign of A.
- Numbers may have up to 100 characters; a 0x prefix, spaces, commas and underscores are ignored. Results use capital letters.

## Worked examples

1. a = 8AB, op = add, b = B78 gives hex = 1423, decimal = 5,155, aDecimal = 2,219, bDecimal = 2,936, binary = 1010000100011. Source: Place values 16ⁿ (Wikipedia, "Hexadecimal", https://en.wikipedia.org/wiki/Hexadecimal).
2. a = 8AB, op = sub, b = B78 gives hex = -2CD, decimal = -717.
3. a = FF, op = mul, b = 10 gives hex = FF0, decimal = 4,080.
4. a = B78, op = div, b = 8AB gives hex = 1, remainder = 2CD, decimal = 1.
5. a = -1A, op = div, b = 4 gives hex = -6, remainder = -2, decimal = -6.
6. a = FFFFFFFFFFFFFFFF, op = add, b = 1 gives hex = 10000000000000000, decimal = 18446744073709551616.
7. a = 8AB, op = add, b = B78, dec = 48879 gives decHex = BEEF.

## FAQ

### How do I add hex numbers?

Add column by column from the right, as in decimal, but carry when a column reaches 16. For 8AB + B78: B + 8 = 11 + 8 = 19 = 16 + 3, write 3 carry 1; A + 7 + 1 = 18 = 16 + 2, write 2 carry 1; 8 + B + 1 = 20 = 16 + 4, write 4 carry 1; so the sum is 1423.

### How do I convert hex to decimal?

Multiply each digit by its place value, a power of 16, and add. The letters stand for 10 (A) to 15 (F). 8AB = 8 × 256 + 10 × 16 + 11 = 2,048 + 160 + 11 = 2,219.

### How do I convert decimal to hex?

Divide by 16 again and again and read the remainders from the last to the first. 48,879 ÷ 16 = 3,054 remainder 15 (F); 3,054 ÷ 16 = 190 remainder 14 (E); 190 ÷ 16 = 11 remainder 14 (E); 11 is B. So 48,879 is BEEF. Use the Convert decimal to hex box.

### How does hex division work here?

It is whole-number division: the quotient is rounded toward zero and the remainder is what is left, A − quotient × B. B78 ÷ 8AB is 1 remainder 2CD, because 2,936 = 1 × 2,219 + 717 and 717 is 2CD in hex.

### How are negative results shown?

With a minus sign in front, such as −2CD for 8AB − B78. The calculator does not use two’s complement, which depends on a fixed number of bits.

### How big can the numbers be?

Up to 100 characters each. The arithmetic uses exact whole numbers, so FFFFFFFFFFFFFFFF + 1 gives exactly 10000000000000000 (2⁶⁴), with no rounding.

## Sources

- Wikipedia, Hexadecimal (base 16, digits 0–9 and A–F, place values powers of 16, conversion by repeated division). https://en.wikipedia.org/wiki/Hexadecimal
- Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed., §4.1 Positional number systems, and §4.3.1 (classical multiple-precision addition, subtraction, multiplication and division).
