Python100例 我的实现展示(51-55例)
'''51、学习使用按位与 & 。'''
def test_exam_51():
a = 58 # a = 0011 1010
b = 11 # b = 0000 1011
c = 0 # c = 0000 0000
c = a & b # c = 0000 1010 = 10(ten)
print(c)
'''52、学习使用按位或 |。'''
def test_exam_52():
a = 58 # a = 0011 1010
b = 11 # b = 0000 1011
c = 0 # c = 0000 0000
c = a | b # c = 0011 1011 = 59(ten)
print(c)
'''53、学习使用按位异或 ^ 。'''
def test_exam_53():
a = 58 # a = 0011 1010
b = 11 # b = 0000 1011
c = 0 # c = 0000 0000
c = a ^ b # c = 0011 0001 = 49(ten)
print(c)
'''54、取一个整数a从右端开始的4〜7位。'''
def test_exam_54():
s = input("请输入7位数以上的整数,程序将取从右端开始的4〜7位并输出该数\n")
if len(s) >= 7:
str1 = s[-4: -8: -1]
print(str1)
else:
print("整数的长度小于7,请重新输入位数大于7的整数。")
'''55、学习使用按位取反~。'''
def test_exam_55():
a = 58 # a = 0011 1010
c = 0 # c = 0000 0000
c = ~a # c = 1100 0101 = -59(ten)
print(c)
if __name__ == '__main__':
# test_exam_51()
# test_exam_52()
# test_exam_53()
# test_exam_54()
# test_exam_55()