Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- twos = input("Please Enter an 8 digit Twos Complement Number")
- if len(twos)!=8:
- print("Invalid length")
- #return
- print(len(twos))
- for i in twos:
- if int(i) > 1:
- print("invalid value")
- #return
- if twos[0]=="1":
- print("negative twos complement")
- twos = int(twos, 2)
- decimal = 256 - twos
- print("decimal: -",decimal)
- hexa=(hex(decimal))
- print("Hex: -",hexa[2::])
- else:
- print("positive twos complement")
- number = int(twos,2)
- print("decimal:",number)
- hexa=(hex(number))
- print("Hex:",hexa[2::])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement