Java results differ for (int)Math.pow(2,x) and 1<<x
Why do the following two operations yield different results in Java for x = 31 or 32 but the same results for x=3 ? int x=3; int b = (int) Math.pow(2,x); int c = 1<<x; Results: x=32: b=2147483647; c=1; x=31: b=2147483647; c=-2147483648; x=3: b=8 ; c=8 There are multiple issues at play: An int can only store values between -2147483648 and 2147483647 . 1 << x only uses the lowest five bits of x . Thus, 1 << 32 is by definition the same as 1 << 0 . Shift operations are performed on the two's-complement integer representation of the value of the left operand ; this explains why 1 << 31 is negative