File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- #### ** Check if the integer is even or odd**
1+ #### ** 1. Check if the integer is even or odd**
22```
33if ((x & 1) == 0) {
44 x is even
@@ -8,7 +8,7 @@ if ((x & 1) == 0) {
88```
99<br />
1010
11- #### ** Test if the n-th bit is set**
11+ #### ** 2. Test if the n-th bit is set**
1212```
1313if (x & (1 << n)) {
1414 n-th bit is set
@@ -18,49 +18,49 @@ if (x & (1 << n)) {
1818```
1919<br />
2020
21- #### ** Set the n-th bit**
21+ #### ** 3. Set the n-th bit**
2222```
2323y = x | (1 << n)
2424```
2525<br />
2626
27- #### ** Unset the n-th bit**
27+ #### ** 4. Unset the n-th bit**
2828```
2929y = x & ~(1 << n)
3030```
3131<br />
3232
33- #### ** Toggle the n-th bit**
33+ #### ** 5. Toggle the n-th bit**
3434```
3535y = x ^ (1 << n)
3636```
3737<br />
3838
39- Turn off the rightmost 1-bit,将最右边的1清零
39+ #### ** 6. Turn off the rightmost 1-bit,将最右边的1清零**
4040```
4141y = x & (x - 1)
4242```
4343<br />
4444
45- Isolate the rightmost 1-bit,留下最右边的1,其余位都置为0
45+ #### ** 7. Isolate the rightmost 1-bit,留下最右边的1,其余位都置为0**
4646```
4747y = x & (-x)
4848```
4949<br />
5050
51- Right propagate the rightmost 1-bit,将最右边的1的右边全部置为1
51+ #### ** 8. Right propagate the rightmost 1-bit,将最右边的1的右边全部置为1**
5252```
5353y = x | (x - 1)
5454```
5555<br />
5656
57- Isolate the rightmost 0-bit,将最右边的0置为1,其余位都置为0
57+ #### ** 9. Isolate the rightmost 0-bit,将最右边的0置为1,其余位都置为0**
5858```
5959y = ~x & (x + 1)
6060```
6161<br />
6262
63- Turn on the rightmost 0-bit,将最右边的0置为1
63+ #### ** 10. Turn on the rightmost 0-bit,将最右边的0置为1**
6464```
6565y = x | (x + 1)
6666```
You can’t perform that action at this time.
0 commit comments