Python Exercise Example 53

Python 100例Python 100 Examples

Title:Learn to use the bitwise XOR ^ operator.

Program analysis:0^0=0; 0^1=1; 1^0=1; 1^1=0

Program source code:

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    a = 0o77
    b = a ^ 3
    print ('The a ^ 3 = %d' % b)
    b ^= 7
    print ('The a ^ b = %d' % b)

The output of the above example is:

The a ^ 3 = 60
The a ^ b = 59

Python 100例Python 100 Examples

Other extensions