Skip to main content

Hexadecimal Arithmetic

Hexadecimal Arithmetic (or Base-16 Math).It is the standard language of memory addressing in computer science.

Here is exactly how to do it step-by-step, just like you would with normal decimal numbers, but using 16 digits instead of 10.

The Basics: Base-16

In our normal world (Decimal), we count 0-9. In the computer world (Hex), we count 0-9 and then A-F.

  • A = 10
  • B = 11
  • C = 12
  • D = 13
  • E = 14
  • F = 15

Calculation 1: Finding the End of the Section

Problem: 0x13EA00 (Size) + 0x2000 (Start Address)

Stack them up like normal addition:

1 3 E A 0 0
+ 2 0 0 0
---------------
  1. Right to Left:

    • 0 + 0 = 0
    • 0 + 0 = 0
    • A + 0 = A
  2. The Tricky Part:

    • E + 2
    • Remember: E is 14.
    • 14 + 2 = 16.
    • In Hex, 16 is written as 10 (just like how 'ten' is written as 10 in decimal).
    • So, write down 0 and carry the 1.
  3. Finish it:

    • 3 + 1 (carry) = 4
    • 1 + 0 = 1

Result: 1 4 0 A 0 0 (or 0x140A00).

Calculation 2: Distance from the End

Problem: 0x140A00 (End) - 0x1409AA (Entry Point)

Stack them up for subtraction:

   1 4 0 A 0 0
- 1 4 0 9 A A
---------------
  1. Right Column: 0 - A. You can't do 0 - 10. So you must borrow.
    • The neighbor is also 0, so borrow from the next neighbor (A).
    • This A becomes 9. The middle 0 becomes F (15), and the right 0 becomes 16 (in value).
    • Now: 16 - 10 (A) = 6.
  2. Middle Column:
    • We have F (15) from the borrow.
    • 15 (F) - 10 (A) = 5.
  3. Left Columns:
    • The A became 9 from the borrow. 9 - 9 = 0.
    • 0 - 0 = 0... and so on.

Result: 0x000056.

To convert 0x56 to decimal (normal numbers):

  • Take the first digit (5) multiplied by 16: 5 * 16 = 80.
  • Add the second digit (6): 80 + 6 = 86.

Answer: 86 bytes.


Pro Tip

You don't have to do this by hand! You can use Python right in your terminal:

# Calculate End Address
python3 -c "print(hex(0x2000 + 0x13ea00))"

# Calculate Distance
python3 -c "print(0x140a00 - 0x1409aa)"

# Convert Hex to Decimal
python3 -c "print(int('56', 16))"