init
This commit is contained in:
36
Bit Setting in C.md
Normal file
36
Bit Setting in C.md
Normal file
@ -0,0 +1,36 @@
|
||||
|
||||
|
||||
#### Setting a bit
|
||||
Use the bitwise OR operator ( | ) to set a bit.
|
||||
```c
|
||||
number |= 1 << x;
|
||||
```
|
||||
That will set bit x.
|
||||
|
||||
#### Clearing a bit
|
||||
Use the bitwise AND operator (&) to clear a bit.
|
||||
```c
|
||||
number &= ~(1 << x);
|
||||
```
|
||||
That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.
|
||||
|
||||
#### Toggling a bit
|
||||
The XOR operator (^) can be used to toggle a bit.
|
||||
```c
|
||||
number ^= 1 << x;
|
||||
```
|
||||
That will toggle bit x.
|
||||
|
||||
### Checking a bit
|
||||
To check a bit, shift the number x to the right, then bitwise AND it:
|
||||
```c
|
||||
bit = (number >> x) & 1;
|
||||
```
|
||||
That will put the value of bit x into the variable bit.
|
||||
|
||||
#### Changing the nth bit to x
|
||||
Setting the nth bit to either 1 or 0 can be achieved with the following:
|
||||
```c
|
||||
number ^= (-x ^ number) & (1 << n);
|
||||
```
|
||||
Bit n will be set if x is 1, and cleared if x is 0.
|
||||
Reference in New Issue
Block a user