+1 vote
218 views
in Java Interview Question by (330 points)
recategorized by
This question was asked me to an interview, can you tell me the exact answer?

 

1 Answer

0 votes
by
n && n == (n & (~n + 1))

One's complement machines will only work with the above expression. If you're only working with two's complement machines where negation may take the same or fewer clock cycles, assuming that even matters, it can be shortened to the following:

n && n == (n & -n)

I'd create a C program iterating -64 to +64 inclusive that prints the binary values as well as decimal to see why this works.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated