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= 10B= 11C= 12D= 13E= 14F= 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
---------------
-
Right to Left:
0 + 0 = 00 + 0 = 0A + 0 = A
-
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
0and carry the1.
-
Finish it:
3 + 1 (carry) = 41 + 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
---------------
- Right Column:
0 - A. You can't do0 - 10. So you must borrow.- The neighbor is also
0, so borrow from the next neighbor (A). - This
Abecomes9. The middle0becomesF(15), and the right0becomes16(in value). - Now:
16 - 10 (A) = 6.
- The neighbor is also
- Middle Column:
- We have
F(15) from the borrow. 15 (F) - 10 (A) = 5.
- We have
- Left Columns:
- The
Abecame9from the borrow.9 - 9 = 0. 0 - 0 = 0... and so on.
- The
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))"