Inverting bits of binary string java

user3259851 picture user3259851 · Mar 25, 2014 · Viewed 7.5k times · Source

I want to invert the bits in binary string. For example String 000 should be inverted as 111 and 1101 as 0010. Can anybody help on this?

Answer

Joop Eggen picture Joop Eggen · Mar 25, 2014
s = s.replace('0', '2').replace('1', '0').replace('2', '1');