$bits =~ s[(..)][ ## Look at every pair of bits $1 eq '00' ## If they are 00 ? '11' ## replace them 11 : $1 eq '11' ## If they are 11 ? '00' ## replace them with 00 : $1 ## else leave them alone ]ge;