there are 2 ways to look at it..
1) you have n bits, and bits 1 & 2 (for example) are not trusted). The LSB is &4.
2) you have n bits, and only the highest z are good. find the first set bit, and count towards the 1's position until you find z or run out (if you run out all are good).
example brute force / crude / fun solution prints the number in binary and again with only N significant bits. You can do it with less code, but it would quickly become hard to follow if you get cute at it.
int main()
{
int soi = 8*sizeof (unsignedlonglong); //in bits
unsignedlonglong testnum = 31415926539;
unsignedlonglong u = 9223372036854775808ull; //2^63
int j;
for(j = 0; j++ < soi; u/= 2)
{
cout << (int)((testnum&u) > 0);
}
cout << endl; //written in binary for fun.
u = 9223372036854775808ull; //2^63
int sigb = 5;
int s = 0;
bool b = false;
for(j = 0; j++ < soi; u/= 2)
{
if(testnum&u)
b = true;
if(b && s < sigb)
{
s++;
cout << (int)((testnum&u) > 0); //the last one this line prints is what you asked.
//its location is &u, or you can convert that to the nth bit easily.
}
else
{
cout << '0';
}
}
cout << endl;
}
which is kind of nuts. some messing with this would get you there, just handle negatives and roundoff carefully to be sure its always correct:
cout << (int)(log2(testnum)+0.999999) << endl; //the most sig bit of the number, from the 1's bit. so that minus the number of bits you want is the least... etc...