M
Mick_fae_Glesga
OK, the solution to this is probably blindingly obvious to everyone,
but... surely it can't be right.
I am compiling with borland bcc32 free compiler
this piece of code is designed to identify the most significant bit in
a given element in an array of unsigned longs. Now I realise there may
be a more efficient way to do this, and if you know a better way please
let me know.
However, the efficieny of the code is neither here nor there, the
point is the weird result:
// an array of two unsigned longs
unsigned long twoULongs[2];
// initial amount to shift by
unsigned long rShift = 0;
// fill the lowest element of the array with its maximum
// possible value
twoULongs[0] = 0xFFFFFFFF;
twoULongs[1] = 0;
// this loop should end when we have discovered the most significant
bit in
// element 0 of the array storing the bit number in
rShift
// (actually, the bit position + 1)
while((twoULongs[0] >> rShift) != 0)
{
// Display the value of the shift (for debug purposes)
cout << (twoULongs[0] >> rShift) << endl;
// since we haven't found the msb yet, shift by one more next time
rShift++;
// pause the program so we can see whats going on for debug
// purposes
cin.get();
}
Now the output, as twoULongs[0] tends towards 0 is:
31, 15, 7, 3, 1, 4294967295 !!!
Whoa!! we just ticked over back to the highest possible unsigned
long!!!
So why didn't it hit 0 after 1?
please rate my idiocy, and if you know whats wrong, tell me
but... surely it can't be right.
I am compiling with borland bcc32 free compiler
this piece of code is designed to identify the most significant bit in
a given element in an array of unsigned longs. Now I realise there may
be a more efficient way to do this, and if you know a better way please
let me know.
However, the efficieny of the code is neither here nor there, the
point is the weird result:
// an array of two unsigned longs
unsigned long twoULongs[2];
// initial amount to shift by
unsigned long rShift = 0;
// fill the lowest element of the array with its maximum
// possible value
twoULongs[0] = 0xFFFFFFFF;
twoULongs[1] = 0;
// this loop should end when we have discovered the most significant
bit in
// element 0 of the array storing the bit number in
rShift
// (actually, the bit position + 1)
while((twoULongs[0] >> rShift) != 0)
{
// Display the value of the shift (for debug purposes)
cout << (twoULongs[0] >> rShift) << endl;
// since we haven't found the msb yet, shift by one more next time
rShift++;
// pause the program so we can see whats going on for debug
// purposes
cin.get();
}
Now the output, as twoULongs[0] tends towards 0 is:
31, 15, 7, 3, 1, 4294967295 !!!
Whoa!! we just ticked over back to the highest possible unsigned
long!!!
So why didn't it hit 0 after 1?
please rate my idiocy, and if you know whats wrong, tell me