Hi, been working on a code for the past hours, but on the last hour I have been stuck with this bug:

it works all and good, but when I reached the decimal number **-128**.

The function wont work. only that number. I realized on my code why it is like that.

The problem is, I cant fix it without changing the whole code, and I dont really have another idea on how to make it work… I will be happy to get tip on how to fix the bug if it is possible.

The Question is:

dec_num (compliment_twos) - the function accepts as number an input in decimal representation and returns as output the

Complements the number 2 in binary representation as a string, in whole byte, with the leftmost bit representing

Is the number negative or not. Note: A positive number with the leftmost (MSB) lit will be completed forever

Sorry for bad translation, not very much good of english of mine + google translate ( it was in hebrew )

```
def pad_zeros(binary_as_str, length):
subtraction = length - len(binary_as_str)
return "0" * subtraction + binary_as_str
def binary_addition(binary_as_str1, binary_as_str2):
counter , sm_in_string= 0, ""
if len(binary_as_str1) < len(binary_as_str2):
binary_as_str1 = pad_zeros(binary_as_str1, len(binary_as_str2))
elif len(binary_as_str2) < len(binary_as_str1):
binary_as_str2 = pad_zeros(binary_as_str2, len(binary_as_str1))
for i in range(len(binary_as_str1) - 1, - 1, - 1):
if binary_as_str1[i] == "1":
counter += 1
if binary_as_str2[i] == "1":
counter += 1
sm_in_string = str(counter % 2) + sm_in_string
if counter >= 2:
counter = 1
else:
counter = 0
if counter % 2 == 1:
return "1" + sm_in_string
else:
return sm_in_string
def round_to_zero(binary_as_str):
counter = 0
second_counter = 0
for i in range(8, len(binary_as_str) + 1):
if len(binary_as_str) < 8:
return binary_as_str
else:
if i % 8 == 0: # 8
counter += 8
else: # 9, 10, 11
counter += 1
second_counter += 1 # 1, 2, 3
rounded = counter + (8 - second_counter) # 11
return rounded
def decimal_to_binary(decimal_num):
binary = "" # It will increase as the loop continues, will increase by "0" or "1".
counter = decimal_num # The decimal number will be decreased with each run.
while counter != 0: # If it is equal to zero it means the transformation has ended.
if counter % 2 == 0:
counter = counter // 2 # Floor division, e.g: 29.5 goes to 29.
binary = "0" + binary
else:
counter = counter // 2 # Floor division, e.g: 29.5 goes to 29.
binary = "1" + binary
if decimal_num == 0:
binary = "0"
return binary # Returns the binary in string after all the process
def twos_compliment(num_dec):
str1 = ""
if num_dec > 0:
binary_in_string = pad_zeros(decimal_to_binary(num_dec), round_to_zero(decimal_to_binary(num_dec))) # 1100100
for i in range(len(binary_in_string)): # 7-1=6
if binary_in_string[i] == "0":
str1 = str1 + "1"
else:
str1 = str1 + "0"
str1 = binary_addition(str1, "1")
else:
decimal = decimal_to_binary(num_dec * (-1))
length = round_to_zero(decimal_to_binary(num_dec * (-1)))
return pad_zeros(decimal, length)
return str1
print(twos_compliment(-128)) # 10000000 - PROBLEM!!
print(twos_compliment(255)) # 1111111100000001
print(twos_compliment(64)) # 11000000
#My Tests:
print(twos_compliment(100)) # 10011100
print(twos_compliment(45)) # 11010011
print(twos_compliment(-45)) # 00101101
print(twos_compliment(-168)) # 0000 0000 1010 1000
print(twos_compliment(-256)) # 0000000100000000
print(twos_compliment(2500)) # 1111011000111100
print(twos_compliment(-127)) # 01111111
```