If you want to flip the bit of the num numeric value without first converting it to an array of characters '0' and '1' , then you can use functions such as BITXOR , BITGET and BITSET (as Andrey also mentions ):
num = bitxor(num, 4); %
However, if you want to work with an array of characters, you can also do this very weirdly:
X(i) = 'a' - X(i); %
This works because characters 'a' and X(i) first converted to their Unicode equivalent UTF-16 numeric values ββbefore the mathematical operation is performed. Since the numerical value for 'a' is 97, then a '0' (numerical value 48) or '1' (numerical value 49) subtracted from 'a' will result in a numerical value for the other. The resulting numerical value on the right side of the equation is then converted back to the character when it is put back into the character array X
source share