Untitled
unknown
plain_text
a year ago
1.2 kB
4
Indexable
def send(msg): length = len(msg) # Splitting the message into two halves x = msg[:length // 2] y = msg[length // 2:] # Performing binary addition convert = add_binary(x, y) # If there's a carry, we need to handle it separately if len(convert) > len(x): convert = convert[1:] convert = add_binary(convert, "1") # Performing 1's complement onecom = ''.join(['1' if ch == '0' else '0' for ch in convert]) # Appending the 1's complement to the original message result = msg + onecom print("Output:", result) def add_binary(x, y): max_len = max(len(x), len(y)) # Padding the shorter string with leading zeros x = x.zfill(max_len) y = y.zfill(max_len) result = [] carry = 0 # Performing binary addition for i in range(max_len - 1, -1, -1): bit_sum = carry + int(x[i]) + int(y[i]) result.append(str(bit_sum % 2)) carry = bit_sum // 2 if carry: result.append('1') result.reverse() return ''.join(result) if __name__ == "__main__": msg = input("Enter the message: ") send(msg)
Editor is loading...
Leave a Comment