Doijashvili Giorgi Jgufi4 Davaleba2

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

number = input("Number: ")

number_decimal = abs(int(number))
bit = int(input("Enter number of bits: "))
bits = "0" * bit
number_to = input("Enter: SM, 1, 2, EXC: ")

list_rev = []

while abs(number_decimal) > 0:


number_decimal = int(number_decimal)
rem = number_decimal % 2
number_decimal = number_decimal // 2
list_rev.append(rem)
final_list = list_rev[::-1]
binary_number = "".join([str(i) for i in final_list])

length = len(binary_number)

n_bit_binary = bits + binary_number

n_bit_binary = n_bit_binary[length:]

SM = n_bit_binary[1:]
positive = "0" + SM
negative = "1" + SM

ones_complement = n_bit_binary.replace("1","Z")
ones_complement = ones_complement.replace("0","1")
ones_complement = ones_complement.replace("Z", "0")

bin_to_dec = int(ones_complement,2)

new_dec = bin_to_dec + 1
twos_complement = bin(new_dec)
twos_complement = twos_complement[2:]

EXCESS = list(twos_complement)
if EXCESS[0] == "1":
EXCESS[0] = "0"
EXCESS = "".join(EXCESS)

POSITIVE_EXCESS = list(n_bit_binary)

if POSITIVE_EXCESS[0] == "0":
POSITIVE_EXCESS[0] = "1"
POSITIVE_EXCESS = "".join(POSITIVE_EXCESS)

if number_to == "SM" and int(number) > 0:


print(positive)
if number_to == "SM" and int(number) < 0:
print(negative)
if number_to == "SM" and int(number) == 0:
print('{} or {}'.format(negative, positive))

if number_to == "1" and int(number) < 0:


print(ones_complement)
if number_to == "1" and int(number) > 0:
print(n_bit_binary)
if number_to == "1" and int(number) == 0:
print('{} or {}'.format(ones_complement, n_bit_binary))

if number_to == "2" and int(number) < 0:


print(twos_complement)
if number_to == "2" and int(number) >= 0:
print(n_bit_binary)

if number_to == "EXC" and int(number) < 0:


print(EXCESS)
if number_to == "EXC" and int(number) >= 0:
print(POSITIVE_EXCESS)

You might also like