How to reset bits in my variables?

May 18, 2016 at 10:48pm
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
class bit
{
public:
	unsigned int x : 3; // 000 001 010 011 100 101 110 111 (0...7)
};

int main()
{
	bit bitObj;
	bitObj.x = 3;
	cout << bitObj.x << endl; //This prints out 3
        return 0;
}


How can I clear bitObj.x? How can I apply the number&= (1 << x) formula to it?

Last edited on May 18, 2016 at 10:48pm
May 19, 2016 at 8:05am
You can use the bitwise negation (~).

number&= ~(1 << x)

What do you mean by clear? Just assign your default value.
bitObj.x = 0b0;
May 19, 2016 at 8:16am
You can use bitObj.x like any other numeric variable, so:

bitObj.x &= (1 << x); // Set a single bit and clear the other
Topic archived. No new replies allowed.