整数移位操作
功能要求
取一个整数a从右端开始的4〜7位。
实例代码
a = int(input('input a number:'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print('a = %o\tb = %o\tc = %o\td = %o' % (a,d,c,d))
运行结果
input a number:9 #从键盘上输入数字“9”
a = 11 b = 0 c = 17 d = 0
按位取反运算示例
功能要求
使用按位取反~。
实例代码
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 a's 1 complement is -235
The a's 2 complement is -235