You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- bitwise AND `&`: returns `1` for each bit as long as all of the two is `1`, else `0`
98
98
- bitwise XOR `^`: returns `1` if the two bits are different, `0` when identical
99
99
100
-
101
100
## Bitwise on Enum
102
101
103
102
Enum can be flags when the type is marked with `FlagsAttribute` and ordinary enum members are powers of 2(members represent the `All` or `None` are exceptional)
@@ -131,7 +130,7 @@ enum Foo
131
130
Bit mask is a common pattern that works like a filter to include or exclude or test bits.
132
131
The mask can refer to a integer represented as binary, a sequence of integers or a matrix of integers. The classical usage is a singular number.
133
132
134
-
### Is Bit Set
133
+
##Bit Checking
135
134
136
135
A common usage of mask is checking whether certain bit **is set**
137
136
@@ -152,8 +151,10 @@ int mask = 1 << position; // generate a special number 0100
152
151
boolpositionIsSet= (number&mask) !=0;
153
152
```
154
153
155
-
This is the particular same case as how we tell whether a union of enum contains a enum flag.
154
+
This is the particularly similar as how we tell whether a union of enum contains a enum flag.
155
+
156
156
Since each enum member has **only one bit set**, the union in the following example has two bits set, only when the set bit from the flag being checked overlaps with any bit of the union can it evaluate to non-zero.
157
+
157
158
And in fact the operation `(union & flag)` should be equal to the flag itself.
158
159
159
160
> [!WARNING]
@@ -177,3 +178,11 @@ enum Foo
177
178
All=~(~0<<4)
178
179
}
179
180
```
181
+
182
+
## Set & Unset & Toggle
183
+
184
+
Similar to checking a bit, setting and unsetting require a same mask but different operation.
0 commit comments