-
bit clearing problem
I'm using the following code to clear a bit in position pos (starting with position 0 on the far right) in the integer value. (Please don't comment on the stupidity of the code writing right now). When I give it an input of 15 and 1 it gives me some massively negative number (-2147483639) as the return value while it should be returning 13
(clearing the first bit would effectively subtract two from the original value)
Any ideas on why this is not working properly?
Code:
unsigned int IBCLR(int value, int pos)
{
unsigned int tempval = (unsigned)value;
unsigned int temppos = (unsigned)pos;
return tempval & (~((unsigned)1 << temppos));
}
-
I know you said not to clean it, but I rewrote it to make sure it was correct...
I got 13 with this code:
Code:
#include <iostream>
using namespace std;
unsigned IBCLR (unsigned value, unsigned pos)
{
return value & (~(1U << pos));
}
int main () {
cout << IBCLR (15, 1) << endl;
cin.get();
}
Yes, your code works.
-
>> it gives me some massively negative number
And yet your return type is unsigned....how are you getting a negative number?
Your code looks fine to me.
gg
-
Let's see his call to it. Possibly something like this?
Code:
unsigned int myfunction( ... );
...
int x;
...
x = myfunction( somestuff );
Quzah.