Python includes exercises 54
Title: Take an integer from a right end of 4~7 bits.
Program Analysis: consider this:
(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 = int(raw_input('input a number:\n')) b = a >> 4 c = ~(~0 << 4) d = b & c print '%o\t%o' %(a,d)
The above example output is:
input a number: 9 11 0