# Accept solution with xor

• ``````update salary set sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex));
``````

• @newdev This is a great solution! The best thing is that I can use this technique in any problem involving switching, whether it's on a database or in code. Thanks a lot.

• @newdev said in Accept solution with xor:

``````update salary set sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex));
``````

• Here is my answer.

``````UPDATE salary SET sex = IF(sex='m','f','m');
``````

• Same idea

``````update salary
set sex = char(ascii(sex) ^ 11);
``````

• @vivek_23 could you tell me why using 11？

• I think it's probably because 11 = 'f' XOR 'm'.

• @mclachlan ok，i agree with u， but why 11 can replace 'f' xor 'm'

• Think of it as: 11 is the difference between 'm' and 'f'. (Like we said earlier, 11 = 'm' ^ 'f'.)

So now that we know the difference, it can be used to convert both an 'm' into an 'f' AND an 'f' into an 'm'.

So,
f = 'm' ^ 11; // This converts an 'm' into an 'f'.
m = 'f' ^ 11; // This converts an 'f' into an 'm'.

• @marietudiant , what @mclachlan saying is correct.

• @mclachlan @vivek_23 highly appreciate your help, thanks.

• @marietudiant
in ascii table, 'f' is `0x66`, 'm' is `0x6D`, and `0x66 xor 0x6D` = `0x0B`, which is 11 in decimal

• Another simple solution

``````update salary set sex= CHAR(ASCII('f') + ASCII('m') - ASCII(sex));
``````

• @abosen said in Accept solution with xor:

IF(sex='m','f','m')

i don't understand IF(..) why?

• @vkcharan This is easy to understand. Good job!

• @abosen nice

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.