Skip to content

Commit 4155c53

Browse files
author
liwentian
committed
fd
1 parent 317adc7 commit 4155c53

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

‎doc/BitSet.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### **Check if the integer is even or odd**
1+
#### **1. Check if the integer is even or odd**
22
```
33
if ((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
```
1313
if (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
```
2323
y = x | (1 << n)
2424
```
2525
<br/>
2626

27-
#### **Unset the n-th bit**
27+
#### **4. Unset the n-th bit**
2828
```
2929
y = x & ~(1 << n)
3030
```
3131
<br/>
3232

33-
#### **Toggle the n-th bit**
33+
#### **5. Toggle the n-th bit**
3434
```
3535
y = 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
```
4141
y = 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
```
4747
y = 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
```
5353
y = 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
```
5959
y = ~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
```
6565
y = x | (x + 1)
6666
```

0 commit comments

Comments
 (0)