How to I change 10101010 to 01010101 ?

andrewgodfrey

New Member
Greetings group.
I would like to know how to replace a 0 with 1, and 1 with 0.

eg.
If variable b1 = 10101010

I want b2 to become 01010101.


Thanks,
Russell.
 

womai

Senior Member
You can use the XOR (exclusive or) function for this:

b0 = b0 XOR 0xff

The truth table for XOR is (for each bit in b0):

0 and 0 --> 0
1 and 0 --> 1
0 and 1 --> 1
1 and 1 --> 0

i.e. it is 1 if either the first or the second input bit is 1, and 0 otherwise.

Wolfgang
 

hippy

Ex-Staff (retired)
XOR also works for all combinations of possible values which need bits changing.
 
Top