a ^ 0b11111111 #exclusive or's each bit in a with 1, inverting each bit
>>> a=0b01100001
>>> bin(a ^ 0b11111111)
'0b10011110'
>>> bin((a ^ 0b11111111) & (b ^ 0b11111111))
'0b10011100'
This is different than using the ~ operator since ~ returns a negative binary result.
>>> bin(~a & ~b)
'-0b1100100
The reason is the ~ operator inverts all bits used in representing the number, including the leading 0's that are not typically displayed, resulting in a 2's complement negative result. By using ^ and the 8 bit binary mask, only the first 8 bits are inverted.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…