how to use bitwise operators with binary strings in python

Question:

so i have binary and i want to XOR every letters, like the example binary from word "maru" :

m = 1101101
a = 1100001
XOR = result1 -> get result 1
r = 1110010
XOR = result2 -> result1 will be XOR with r and got result 2

but i only can get all the binary of the word "maru" = 1101101 1100001 1110010 1110101

i tried to separate every binary to run code:

y=int(a,2) ^ int(b,2)  # a is first binary, b is second binary
print('{0:b}'.format(y))  # result from XOR a and b

can someone help me to get the result using python with flexible input?

Asked By: HelpMe

||

Answers:

The critical piece you’re missing is that an integer in python is a native type and is always stored in binary, but represented and interpreted in base 10 unless explicitly instructed. So when you create a literal 1101101 this is the integer number 1,101,101 (one million, one hundred thousand etc).

To convert numbers from binary to integers, create them as strings and then convert them to integers, instructing the int constructor to interpret the strings as base 2 explicitly. Then do the math with integers, then convert them back to binary-formatted strings if you’d like to display them as such:

In [10]: m = int("1101101", 2)
   ....: a = int("1100001", 2)

In [11]: r = int("1110010", 2)

In [12]: result = m ^ a

In [13]: f"{result:b}"
Out[13]: '1100'

In [14]: result2 = result ^ r

In [15]: f"{result2:b}"
Out[15]: '1111110'
Answered By: Michael Delgado

There are couple things to fix. Main one, when you declare variable as:

m = 1101101

it means m is equal to 1 milion, 101 tousands, 101.

You should write it like this:

m = 0b1101101
or
m = 109
or
m = 0x6D

or even better, to not check or remember binary represetnation you just could write:

m = ord("m")

Now we have valid m letter value. Python does not have built-in byte xor operation, but it’s pretty easy to write.

def XOR(char1: int, char2: int) -> int:
    return char1 ^ char2

Let’s declare m, a, calculate XOR and print it:

m = 0b1101101
a = 0b1100001
xor = XOR(m, a)
print(f"m=     {'{0:08b}'.format(m)}na=     {'{0:08b}'.format(a)}naXORm= {'{0:08b}'.format(xor)}")

Console will return something like that:

m=     01101101
a=     01100001
aXORm= 00001100

Now the last thing is to iterate over "maru" string. It can be done like that:

string_to_xor = "maru"
xor_of_string = ord(string_to_xor[0])
for i in range(len(string_to_xor)-1):
    print(f"{chr(xor_of_string)} ({bin(xor_of_string)}) XOR {string_to_xor[i+1]} ({bin(ord(string_to_xor[i+1]))}) is:")
    xor_of_string = XOR(xor_of_string, ord(string_to_xor[i+1]))
    print('{0:08b}'.format(xor_of_string))

Xor after each letter will be printed. Last one is ascii value of each letter XORed in binary format.

Console output:

m (0b1101101) XOR a (0b1100001) is:
00001100
 (0b1100) XOR r (0b1110010) is:
01111110
~ (0b1111110) XOR u (0b1110101) is:
00001011
Answered By: LesniakM
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.