Python按位异或

a=3
b=100
print("将其二进制反过来输出:")
print("a反:",bin(a)[2:][::-1])
print("b反:",bin(b)[2:][::-1])
temp=bin(a^b)[2:]
print("按位异或后:",temp[::-1])
print("将上面的反过来:{}".format(temp))
print("转为10进制:{}".format(a^b))

输出:

将其二进制反过来输出:
a反: 11
b反: 0010011
按位异或后: 1110011
将上面的反过来:1100111
转为10进制:103

结论:转化为2进制后长短不同的问题不用管,python会自动处理

猜你喜欢

转载自blog.csdn.net/weixin_43732022/article/details/131068872
今日推荐