Hanno Fietz - 1 year ago 89

Java Question

I'm storing bit patterns of unsigned 64-bit numbers in a

`long`

`long`

`a - b`

`// on the unsigned range, these numbers would be adjacent`

long a = 0x7fffffffffffffffL;

long b = 0x8000000000000000L;

// but as two's complement (or any representation that

// stores the sign in the first bit), they aren't

assert b - a == 1;

What's the correct way to do this?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

If you're dealing with addition and subtraction, it doesn't matter whether you're using signed or unsigned types, as long as the arguments are both signed or both unsigned. If you need to compare a and b, compare a-b to 0.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**