0

So I am wondering if a binary string such as:

01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001 

(Hello Mom!)
can be reversed in the way that ALL 1s turn into 0s and vice verca in Python 3.

6
  • That is called bitwise xor or ^ operator. Commented Sep 13, 2017 at 0:55
  • 4
    @AChampion: Bitwise xor doesn't work on a string. Commented Sep 13, 2017 at 0:56
  • 1
    @KenWhite You can just convert to int(n, 2) ^ 0xff. Commented Sep 13, 2017 at 1:05
  • 1
    @AChampion: Yes, I'm aware. You didn't explain that; you suggested using ^ on a string, and I pointed out that will not work. Commented Sep 13, 2017 at 1:08
  • 2
    I rolled back your edit. You cannot edit your post to ask an additional question, especially after this one has been answered. If you now have another question, first search this site thoroughly to see if it's been asked and answered before. If it has not, then click the Ask Question button and create a new post to ask it. Commented Sep 13, 2017 at 1:10

4 Answers 4

3

You can split the binary string and use bitwise manipulation, e.g.:

In []:
x = '01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001'
r = [format(int(n, 2) ^ 0xff, 'b') for n in x.split()]
r

Out[]:
['10110111', '10011010', '10010011', '10010011', '10010000',
 '11011111', '10110010', '10010000', '10010010', '11011110']

It's simple enough to join this back up:

In []:
' '.join(r)

Out[]:
'10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110'
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please provide feedback if you vote down, it's helps!
2

You can use join with a generator, and replace the '1' with '0's and vice versa, while keeping any character that is neither (a space in your example) unchanged:

string = "01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
reverse = ''.join('0' if c=='1' else ('1' if c=='0' else c) for c in string)
print(reverse)

Outputs:

10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110

As commented by @AChampion, you could shorten and improve the above code using:

string = "01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
reverse = ''.join('10'[int(c)] if c in '01' else c for c in string)
print(reverse)

2 Comments

''.join('10'[int(c)] for c in string) would also work.
@AChampion You're right, thanks, I included your suggestion in my answer.
2

str.translate() is very useful, and efficient, for this:

bits = '01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001'

table = {ord('0'): '1', ord('1'): '0'}    # '0' -> '1', '1' -> '0'
inverted_bits = bits.translate(table)
print(inverted_bits)

Output:

10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110

This is also handy if you wanted to apply other transformations. For example the spaces could also be removed with this translation table:

table = {ord('0'): '1', ord('1'): '0', ord(' '): None}
print(bits.translate(table))

Output:

10110111100110101001001110010011100100001101111110110010100100001001001011011110

Also, use str.maketrans() to create the translation table:

>>> table = str.maketrans('01', '10')
>>> print(bits.translate(table))
10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110

or

>>> print(bits.translate(str.maketrans('01', '10', ' ')))
10110111100110101001001110010011100100001101111110110010100100001001001011011110

Comments

1
a="01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
a=a.replace('0','a').replace('1','0').replace('a','1')

You could use the replace function.

Output:

'10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110'

2 Comments

Nice "workaround" method without getting into actual binary operators.
Use str.translate() to avoid any "workaround" which requires that the string be processed 3 times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.