Problems in Java Hex Numbers
I was trying to implement Hex calculations on two hex numbers, I am facing
a weird problem
When I tried adding two positive hex numbers, it gave me a negative
result. Here delta =9e3779b9 and sum=0
Here the addition must be 9E3779B9 or 2654435769 in decimal, but the code
in Java makes it negative. Here is the code:
long delta = 0x9e3779b9;
long sum = 0;
sum = sum + delta;
System.out.println(sum);
sum = sum << 5;
System.out.println(sum);
having output as
-1640531527
-52497008864
I believe this is due to the first sign bit for long, how to solve this
problem so that it can ignore the negative?
No comments:
Post a Comment