NVIDIA Interview Question
Software Engineer / Developersbool msb(unsigned int N)
{
N= N>>((sizeof(int)*8)-1);
if(N%2) return true;
return false;
}
I think the question is not worded correctly. I think we have to find the highest set bit. If so, the fast solution is to set all bits to the right of the hibit and then subtract as below.
int hibit(unsigned int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return n - (n >> 1);
}
Can we not find MSB of 32 bit integer using
- Anonymous October 03, 2011int MSB = num & ox80000000 ?