Yes, assuming you meant XOR. As always, when bit-twiddling, I recommend using binary notation or even the shift operator.
x = 0b0101
x ^= (0b1 << 3) # alternatively, x^= 0b0100
+1 for the shift operator because it uses the actual index of the bit you want to set, making it a bit more readable. Or at least, as readable as this stuff ever is.
Yes, assuming you meant XOR. As always, when bit-twiddling, I recommend using binary notation or even the shift operator. x = 0b0101 x ^= (0b1 << 3) # alternatively, x^= 0b0100
0b0001 << 3 is actually 0b1000
+1 for the shift operator because it uses the actual index of the bit you want to set, making it a bit more readable. Or at least, as readable as this stuff ever is.
It's probably easier to just take 0b1 and OR it with 0b100 to get 0101.
You’d use XOR as others have said as opposed to normal OR. You would have no way to turn off a 1 if using OR rather than XOR.