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
x - 1
Is X but the lowest set bit is cleared and all other bits to the right are 1
01010000 => 01001111
The ~ operator will invert...
x = 5 # 00000101 in binary
result = ~x # 11111010 in binary (inverted bits)
print(result) # Output: -6
Setting a Bit:
* To set (turn on) a specific bit, use the bitwise OR (|) operation with a mask where the target bit is 1 and all other bits are 0.
* Example: To set the 3rd bit (counting from 0) of x
x = x | (1 << 3)
Clearing a Bit:
* To clear (turn off) a specific bit, use the bitwise AND (&) operation with a mask where the target bit is 0 and all other bits are 1.
* Example: To clear the 3rd bit of x
x = x & ~(1 << 3)
Toggling a Bit:
* To toggle (flip) a specific bit, use the bitwise XOR (^) operation with a mask where the target bit is 1 and all other bits are 0.
* Example: To toggle the 3rd bit of x
x = x ^ (1 << 3)
Checking a Bit
* To check if a specific bit is set (i.e., if it is 1), use the bitwise AND (&) operation with a mask where only the target bit is 1.
* Example: To check if the 3rd bit of x is set
is_set = x & (1 << 3) != 0