6

I have found somewhere a pretty weird number declaration in Java.

double x = 0xap-001;

I am curious why the value of x is 5.0

Valy
  • 573
  • 2
  • 12
  • 2
    http://stackoverflow.com/questions/4546532/java-floating-point-numbers-representation-as-a-hexadecimal-numbers – Mat May 05 '15 at 15:08

1 Answers1

8

p indicates a binary exponentiation. So you have hex 0xa == 10, with a binary exponent of -1 - in other words a shift right or div 2. The result is 10/2 = 5.

BadZen
  • 4,083
  • 2
  • 25
  • 48