Python includes exercises 55
Topic: Learn to use bitwise ~.
Program analysis: 0 = 1 ~; ~ 1 = 0;
(1) the right to make a four.
(2) set up a low-4 are all 1, the rest are all zero numbers. Available ~ (~ 0 << 4)
(3) both of the above will be the & operator.
Source Code:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': a = 234 b = ~a print 'The a\'s 1 complement is %d' % b a = ~a print 'The a\'s 2 complement is %d' % a
The above example output is:
The a's 1 complement is -235 The a's 2 complement is -235