Aug 12 2010
Using XOR to Toggle an Integer Between 1 and 0
If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (^) operator in C to get the job done.
In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo of the application I wanted to pass in alternating values of 1 and 0 as part of a test for a specific use case. Instead of using an if statement in the calling method to decide when to send a 1 or 0, I wrote something similar to the code below:
Continue reading...
Tips by RSS
Tips by Email