I have been searching how to do this but I haven't find the way to do it. There's another way to calculate this difference instead of be counting one by one?
For example:
0x7fffffffe070 - 0x7fffffffe066 = 0x04
0x7fffffffe066 - 0x7fffffffe070 = -0x04
0x7fffffffdbe0 - 0x7fffffffda98 = ????To understand these results let's suppose we open a file with an hex editor and we have the following hex numbers: 8A B7 00 00 FF, with their corresponding hex offsets: 0x7fffffffe066 0x7fffffffe067 0x7fffffffe068 0x7fffffffe069 0x7fffffffe070. The difference of the hex offsets of the numbers 8A and FF is 0x04 because they differ in 4 positions.
02 Answers
The "hex offsets" are just ordinary numbers and have no special rules for subtraction. You're only getting different results because your input is wrong:
we have the following hex numbers: 8A B7 00 00 FF, with their corresponding hex offsets: 0x7fffffffe066 0x7fffffffe067 0x7fffffffe068 0x7fffffffe069 0x7fffffffe070. The difference of the hex offsets of the numbers 8A and FF is 0x04 because they differ in 4 positions.
In hexadecimal, 0x9 is first followed by 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, and only then by 0x10. So if you have 5 contiguous bytes, and the fourth is at 0x7FFF'FFFF'E069, the following one will be at 0x7FFF'FFFF'E06A, not 0x7FFF'FFFF'E070.
In other words, 0x…E070 - 0x…E066 = 0xA is actually the correct result.
I'm not sure, but maybe the question is how to calculate the difference by hand?
You can calculate difference of A-B by rewriting it to additions. You would do this by first flipping/inverting all the bits from B and then adding that value to A. Next you increment the result by 1 and trim (narrow) the result to be just as many digits (or bits) as the original operands were.
Demo with hexadecimal values from his question:
let's calculate 0x7fffffffe070 - 0x7fffffffe066:
7fffffffe070 800000001f99 + (this is 0x7fffffffe066 with all bits inverted)
===============
1000000000009 1 +
=============== A (or 10 decimal, or +0xa if you will)And now let's calculate 0x7fffffffe066 - 0x7fffffffe070:
7fffffffe066
800000001f8f + (this is 0x7fffffffe070 with all bits inverted)
==============
fffffffffff5 1 +
==============
fffffffffff6 (or -10 decimal / -0xa)It's easy to flip all bits in a hex digit once you know that:
0 = F (or 0000 ==> 1111)
1 = E (or 0001 ==> 1110)
2 = D
...
F = 0 (or 1111 ==> 0000)And so on...