OverviewUsing shift in Java and some surprising edge cases.
Getting the signFor the following program, what is the values for n so that I can change the type of x without having to change the code.
// sign is 1 if negative and 0 if non-negative long sign = x >>> n;There is a value you can replace n such that you don't need to know the type of x Similarly, I want to shift the lowest bit to be the highest without knowing what type I am shifting
// sign is MIN_VALUE if odd otherwise 0 long sign = x << n;For bonus points, write formula for all the possible solutions of n (there is more than one)
Is a power of twoHow do you determine an integer is a power of 2. This is a fairly common interview question. Its pretty hard to figure out in an interview I imagine. To save you googling, you can write this in C with
x&&!(x&(x-1))In Java, you can write this using just 0 ~ - & == != . Complete this expression using those operators
long x = boolean isPow2 = x != 0 .....