1.举例说明二进制与十进制之间的转化
2.举例理解正数和负数的原码、补码和反码,在理解的基础上,掌握位运算符的取反操作,并求出-26的取反,写出详细过程
x = 11
print(bin(11))
x = 0b11
print(x)
flag=0
_s=''
def getbin(s):
_s=bin(abs(s))
_s=_s.replace('0b','')
return _s
def rev(s):
s=s.replace('1','2')
s=s.replace('0','1')
s=s.replace('2','0')
return s
def y(s):
print('原码是:%d,%s\t'%(flag,_s))
def f(s):
global _s
if flag==0:
print('反码是:%d,%s\t'%(flag,_s))
return
_s=rev(_s)
print('反码是:%d,%s\t'%(flag,_s))
return
def by(s):
global _s
_s=int(_s,2)+1
_s=getbin(_s)
print('补码是:%d,%s\t'%(flag,_s))
print('移码是:%d,%s\t'%((flag+1)%2,_s))
def out(s):
y(s)
f(s)
by(s)
while True:
try:
s=int(input('请输入一个十进制整数:\n'))
if s>=0:
flag=0
else:
flag=1
_s=getbin(s)
out(s)
print()
except IOError:
break