&是按位逻辑运算符,比如5 & 6,5和6转换为二进制是101和110,此时101 & 110=100,100转换为十进制是4,所以5 & 6=4;
|是按位或逻辑运算符,比如5|6,就是101|110,得到111=7,所以最后结果为7;
^ 是按位异或逻辑运算符,比如5 ^ 6,其实是101^ 110,结果是011,所以5^6的答案是3;

今天做题遇到一个,傻逼的我当成了幂运算。。。。

# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)]
# Embedded file name: 1.py
# Compiled at: 2017-06-03 10:20:43
import base64

def encode(message):
    s = ''
    for i in message:
        x = ord(i) ^ 32
        x = x + 16
        s += chr(x)

    return base64.b64encode(s)


correct = 'XlNkVmtUI1MgXWBZXCFeKY+AaXNt'
flag = ''
print 'Input flag:'
flag = raw_input()
if encode(flag) == correct:
    print 'correct'
else:
    print 'wrong'
# okay decompiling E:\ctf��Ŀ20.11.28\f417c0d03b0344eb9969ed0e1f772091.pyc

解码运算:

import base64

def decode(message):
    str = ''
    s=base64.b64decode(message)
    for i in s:
        x = i-16
        x = x^32
        str += chr(x)
    return str

correct = 'XlNkVmtUI1MgXWBZXCFeKY+AaXNt'
flag = decode(correct)
print(flag)
    

对一个数进行按位异或逻辑运算,只需要对相同数字再进行一次按位异或运算,即可得到原来数字。

本文地址:https://blog.csdn.net/weixin_45253216/article/details/110261383