I am trying to build a function that calculates the checksum of four elements in an 8-bit array. What I am struggling with is the carry and overflow of binary addition. For example:
101
+
101
----
1010
it overflows we wraparound the most left with the most right so it becomes 011.
I am able to do the calculation using bin. I can do the above if the right most value is 0 and the left most value is 1 because in that case we won't have to carry anything to the next value from the right.
What I am struggling with is, something like this scenario:
111
+
110
----
1101
it overflows again but this time we have to wraparound 1(left most)+1(right most) = 10 which means we will take 0 and carry 1 to the next right most (0) in this case. The final result in that case would be = 110
if it was 1 instead of 0 we will get 10 again and we will have to carry 1 to the next value and so on and so forth.
See my code below:
def overFlow(sumN):
# this works if my left most is 1 and my right most is 0
if(sumN[8] == "0"):
print("--------------------")
overflow = bin(int(sumN[0],2) + int(sumN[8],2))[2:]
temp = list(sumN)
temp.pop(0)
temp.pop(7)
temp.append(overflow)
newtemp= ""
for i in temp:
newtemp += i
sumN = newtemp
print(sumN)
print("--------------------")
# else:
# this is where I am stuck
return sumN
def checksum(message):
var1 = message[0][2:]
var2 = message[1][2:]
var3 = message[2][2:]
var4 = message[3][2:]
bit_len = 8
print(len(var1))
sum1 = bin(int(var1,2) + int(var2,2))[2:]
if(len(sum1) > bit_len):
sum1 = overFlow(sum1)
print("var 1 " +var1+"\nvar 2 "+var2+"\nSum "+sum1)
sum2 = bin(int(sum1,2) + int(var3,2))[2:]
if(len(sum2) > bit_len):
sum2 = overFlow(sum2)
print("var 1 " +sum1+"\nvar 2 "+var3+"\nSum "+sum2)
sum3 = bin(int(sum2,2) + int(var4,2))[2:]
if(len(sum3) > bit_len):
sum3 = overFlow(sum3)
print("var 1 " +sum2+"\nvar 2 "+var4+"\nSum "+sum3)
comp = ""
tot_sum = sum3
n = len(tot_sum)
for i in range(n):
if (tot_sum[i]== '0'):
comp+= '1';
if (tot_sum[i]== '1'):
comp+= '0';
print(f"1's complement is = {comp}")
message = ['0b10110110','0b11011100','0b01100111','0b01111101']
checksum(message);
Any help is much appreciated.
UPDATE: The right output for the message above should be as follow:
var1 10110110
var2 11011100
sum1 110010010 "overflow so we take left most and add it with right most" 1+0 = 1
sum1 becomes = 10010011
sum1 10010011
var3 01100111
sum3 11111010 "no overflow, so we do nothing"
sum3 11111010
var4 01111101
101110111 "overflow left most(1) + right most (1) = 10" we take zero and carry one to the next right most number
which is 1 which will give me again 10 so we take zero and carry one until we don't have to carry anymore
the final result should be **01110000**
after 1's compelement it should be **10001111**
and this is my current output:
var 1 10110110
var 2 11011100
Sum 10010011
var 1 10010011
var 2 01100111
Sum 11111010
var 1 11111010
var 2 01111101
Sum 101110111
1's complement or the checksum is = 010001000
printstatements). Where does your wrap-around code differ from what you expect?string.translate1's complement is = 0000111