is it possible to toggle a bit to 0 when it is 1 and to 1 when it is 0? For example,00000000 go through the function will 00000001 and when the 00000001 go through the function again, 00000001 will become 00000000. Is it possible?
Asked
Active
Viewed 1,634 times
-1
Ah Îoon
- 49
- 1
- 8
-
1yes. and you can do it! – john elemans Nov 28 '16 at 01:46
-
Both `b = !b` and `b = 1 - b` will change 0 to 1 and 1 to 0. – Steve Summit Nov 28 '16 at 01:46
1 Answers
0
Certainly. What you want is to perform a logical XOR with 00000001 (this is called a mask):
MASK INPUT OUTPUT
00000001 XOR 00000000 = 00000001
00000001 XOR 00000001 = 00000000
This also makes it possible to toggle more than one bit, e.g., if your mask were 00001001:
MASK INPUT OUTPUT
00001001 XOR 00000000 = 00001001
00001001 XOR 00001001 = 00000000
00001001 XOR 00001000 = 00000001
00001001 XOR 00000001 = 00001000
Leonora Tindall
- 1,391
- 2
- 12
- 30