Commit 2853fa16 authored by KANTAPONG SONG-NGAM's avatar KANTAPONG SONG-NGAM

add

parent 567a5d72
def value(v, pos=4, neg=3):
"""รับ v มาเพื่อหาค่าเป็นเลขฐานสิบ binary -> digital.
โดยค่ากำหนดเริ่มต้น
pos เป็น 4 หลัก
neg เป็น 3 หลัก
sign bit เป็นหลักแรกเสมอ
"""
sign = -1 if v[0] == 1 else 1
return sign*sum([v[i]*2**(4-i) for i in range(1,8)])
def value(v,pos,neg):
# ทดสอบ
v = [0, 0,0,0,1 1,0,0]
print(v)
print(value(v))
v = [1, 0,1,0,1 0,1,1]
print(v)
print(value(v))
\ No newline at end of file
sign = -1 if int(v[0]) == 1 else 1
return sign*sum([int(v[i])*2**(pos-i) for i in range(1,pos+neg+1)])
print(value('0001011',3,3))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment