Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strange result in xor
#1
hello
i am trying to implement an xor test on 2 values
in binary . I receive in return a strange numeric string instead
of the binary I expect in the code below -
what is wrong please?
thanks for your help
mik
===============
import re

str_a ="ABCD"
str_b ="JHGF"

bin_a = ''.join(format(ord(i), '08b') for i in str_a)
bin_b = ''.join(format(ord(i), '08b') for i in str_b)

print(bin_a)
print(bin_b)

exore = int( bin_a) ^ int(bin_b)

print(( exore ) , " ->  binaire xoré   ")

# ---- output : 18991471129435296771915529226  ->  binaire xoré   
Reply
#2
A string such as '0100011110011' is not a binary string. It is a unicode string containing the unicode characters 0 and 1.

Python integers are treated as infinite binary strings. You could change your code in
import re

str_a ="ABCD"
str_b ="JHGF"

bin_a = ''.join(format(ord(i), '08b') for i in str_a)
bin_b = ''.join(format(ord(i), '08b') for i in str_b)

print(bin_a)
print(bin_b)

exore = int( bin_a, 2) ^ int(bin_b, 2)

print(( bin(exore) ) , " ->  binaire xoré   ")

# ---- output : 0b1011000010100000010000000010  ->  binaire xoré
However the following is better
str_a ="ABCD"
str_b ="JHGF"

x = int(str_a.encode('utf8').hex(), 16)
y = int(str_b.encode('utf8').hex(), 16)

print(bin(x), x)
print(bin(y), y)
z = x ^ y
print(bin(z), z)
Output:
0b1000001010000100100001101000100 1094861636 0b1001010010010000100011101000110 1246250822 0b1011000010100000010000000010 185205762
Also have a look at this official documentation page . There are other resources, such as the bitstring module in Pypi.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Hello Gribouillis
Thank you for your quick reply that helps me a lot:
However a detail is puslling me .
It is possible to make an XOR by hand an we can notice that the length
of the result is the same as the two operands , that is not the case
in your answer - I shifted the result on the right to get the correct position.
So what are the missing digits ..
sorry for that numb remark , I am a newcomer to Python and 78 years old
:
01000001010000100100001101000100
01001010010010000100011101000110
???? 1011000010100000010000000010

the same can be noticed on your 2nd example
sorry again to disturb you
mik
Reply
#4
a = 80
b = 10

c = a ^ b


# print it as binary representation with 8 bit
print(f"{c:08b}")

# print it as binary representation with 16 bit
print(f"{c:016b}")
The f before the quotes means format-string. It's used for string formatting.
Documentation about the format specification: https://docs.python.org/3/library/string...formatspec

If you create two integers from binary literals, then the leading zeros are cut off. But they are equal:
a = 0b101
b = 0b0101

if a == b:
    print("a and b are equal")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
You may find this interesting if a and b are literals instead of strings.
a = int.from_bytes(b"ABCD", byteorder="big")
b = int.from_bytes(b"JHGF", byteorder="big")
w = len(f"{max(a, b):b}")
print(f"{a:>{w}b}\n{b:>{w}b}\n{a^b:0>{w}b}")
Output:
1000001010000100100001101000100 1001010010010000100011101000110 0001011000010100000010000000010
Reply
#6
hello gentlemen ,
I will try to make profit of all you tell me .
However you must realise that for a beginner :
print(f"{a:>{w}b}\n{b:>{w}b}\n{a^b:0>{w}b}")
Huh
It is like chineese to me .
Anyway thank you to all
mik
Reply
#7
Look it up. That's how you learn. Everything is covered in the link from DeaD_EyE's post. Play around taking out parts and see how it changes the output. For example, what changes if you remove the {w}? What changes if you swap the > for <?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas's regular expression function result is so strange cools0607 6 3,273 Jun-15-2020, 07:34 AM
Last Post: cools0607

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020