48、輸入兩個一組的十六進位正整數(X,Y),X,Y各數最多為4位(即16 bit),計算每組
數的AND,OR,和(SUM)與差(DIFFERENCE),將X,Y值及運算結果以2進位形式列印
出來。計算和與差時其最高位之進位、借位可以忽略。
測試數據:(48,90),(F3,76),(48,93)(21,74)
程式:
#coding=utf8
"""
Python Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100 # & Binary AND
a|b = 0011 1101 # | Binary OR
a^b = 0011 0001 # ^ Binary XOR
~a = 1100 0011 # ~ Binary Ones Complement
<< Binary Left Shift
>> Binary Right Shift
There are following Bitwise operators supported by Python language
"""
print("48、輸入兩個一組的十六進位正整數(X,Y),X,Y各數最多為4位(即16 bit),計算每組")
print(" 數的AND,OR,和(SUM)與差(DIFFERENCE),將X,Y值及運算結果以2進位形式列印")
print(" 出來。計算和與差時其最高位之進位、借位可以忽略。")
print("========================================================")
print("===X=0 離開程式=========================================")
hexdec2=int('10000',16)
a=1
while(a != 0):
while True:
X=input("請輸入16進制數值 0 < X < 0FFFF: 0x")
try:
X=int(X,16)
except ValueError:
print ('這是不合法的輸入. 請再輸入一次...')
continue
if ( X < hexdec2):
break
#==================
while True:
Y=input("請輸入數值16進制 0 < Y < 0FFFF: 0x")
try:
Y= int(Y,16)
except ValueError:
print ('這是不合法的輸入. 請再輸入一次...')
continue
if ( Y < hexdec2):
break
#==================
if (X==0 or Y==0):
a=0 #exit
print("======================================")
print("十六進制",hex(X),"=10進制",(X))
print("十六進制",hex(Y),"=10進制",(Y))
print("======================================")
print("16進制 and 運算",hex(X & Y),"=",(X & Y),"(10進制)")
print("16進制 or 運算",hex(X | Y),"=",(X | Y),"(10進制)")
print("16進制 xor 運算",hex(X ^ Y),"=",(X ^ Y),"(10進制)")
print("16進制 not 運算",hex(~X),"=",(~X),"(10進制)")
print("16進制 not 運算",hex(~Y),"=",(~Y),"(10進制)")
print("16進制 加法運算",hex(X+Y),"=",(X + Y),"(10進制)")
print("16進制 減法運算",hex(X-Y),"=",(X - Y),"(10進制)")
print("======================================")
print()
#==================
結果:
================= RESTART: D:/程式語言 Python 入門/50題/Ex50-48.py =================
48、輸入兩個一組的十六進位正整數(X,Y),X,Y各數最多為4位(即16 bit),計算每組
數的AND,OR,和(SUM)與差(DIFFERENCE),將X,Y值及運算結果以2進位形式列印
出來。計算和與差時其最高位之進位、借位可以忽略。
========================================================
===X=0 離開程式=========================================
請輸入16進制數值 0 < X < 0FFFF: 0xf3
請輸入數值16進制 0 < Y < 0FFFF: 0x76
======================================
十六進制 0xf3 =10進制 243
十六進制 0x76 =10進制 118
======================================
16進制 and 運算 0x72 = 114 (10進制)
16進制 or 運算 0xf7 = 247 (10進制)
16進制 xor 運算 0x85 = 133 (10進制)
16進制 not 運算 -0xf4 = -244 (10進制)
16進制 not 運算 -0x77 = -119 (10進制)
16進制 加法運算 0x169 = 361 (10進制)
16進制 減法運算 0x7d = 125 (10進制)
======================================
請輸入16進制數值 0 < X < 0FFFF: 0x0
請輸入數值16進制 0 < Y < 0FFFF: 0x3
======================================
十六進制 0x0 =10進制 0
十六進制 0x3 =10進制 3
======================================
16進制 and 運算 0x0 = 0 (10進制)
16進制 or 運算 0x3 = 3 (10進制)
16進制 xor 運算 0x3 = 3 (10進制)
16進制 not 運算 -0x1 = -1 (10進制)
16進制 not 運算 -0x4 = -4 (10進制)
16進制 加法運算 0x3 = 3 (10進制)
16進制 減法運算 -0x3 = -3 (10進制)
======================================
>>>
沒有留言:
張貼留言