-1

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?

Ah Îoon
  • 49
  • 1
  • 8

1 Answers1

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