Skip to content

Commit 580ed60

Browse files
author
liwentian
committed
fd
1 parent fbd314e commit 580ed60

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

doc/BitSet.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
x is odd
77
}
88
```
9-
- 测试nth bit是否设置:`x & (1 << n)`
10-
- 设置nth bit:`x | (1 << n)`
11-
-
9+
- Test if the n-th bit is set
10+
```
11+
if (x & (1 << n)) {
12+
n-th bit is set
13+
} else {
14+
n-th bit is not set
15+
}
16+
```
17+
- Set the n-th bit
18+
```
19+
y = x | (1 << n)
20+
```
21+
- Unset the n-th bit
22+
```
23+
y = x & ~(1 << n)
24+
```
25+
- Toggle the n-th bit
26+
```
27+
y = x ^ (1 << n)
28+
```
29+
- Turn off the rightmost 1-bit
30+
```
31+
y = x & (x - 1)
32+
```
33+
- Isolate the rightmost 1-bit
34+
```
35+
y = x & (-x)
36+
```

0 commit comments

Comments
 (0)