![]() |
Program demonstrates operations of bitwise operators without using bitwise operations - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Program demonstrates operations of bitwise operators without using bitwise operations (/thread-34652.html) |
Program demonstrates operations of bitwise operators without using bitwise operations - ShawnYang - Aug-18-2021 Symbol Operation Operation & and Sets result bit to 1 if both corresponding bits are 1. E.g. 1010 & 1100 is 1000 l or Sets result bit to 1 if one of the two corresponding bits is 1. E.g 1010 & 1100 is 1110 ^ xor Sets result bit to 1 only if one of the corresponding bits is 1. E.g. 1010 & 1100 is 0110 def main(): # input binary1, operator, binary2 = str(input('Enter binary expression: ')).split(' ') # Enter our binary expression output = '' # Output initialized to empty string output = binary1 + binary2 # Concatenate strings to it, not numbers. for i in range(0, len(binary1)): if operator == '&': binary1[i] != binary2[i] if binary1[i]==1 and binary2[i]==1: output = chr(output + str(1)) # Appended 1 is converted to string else: output = output + str(0) # Append 0 when the condition is not true elif operator == '|': if binary1[i]==1 or binary2[i]==1: output = chr(output + str(1)) # Appended 1 is converted to string else: output = output + str(0) # Append 0 when the condition is not true elif operator == '^': if not binary1[i]==0 and binary2[i]==1: output = chr(output + str(1)) # Appended 1 is converted to string else: output = output + 0 # Append 0 when the condition is not true elif operator == '~': if binary1[i]==1 and not binary2[i]==0: output = chr(output + str(1)) # Appended 1 is converted to string else: output = output + 0 # Append 0 when the condition is not true print(output) # This is correct # output main() Current Result: Enter binary expression: 1110 & 1000 Result: 111010000000 Expected Result Enter binary expression: 1110 & 1000 Result: 1000Hi all, the error in my code is that when I input a binary expression 1110 & 1000, I get 101011110000. But I am supposed to get the result 1000. Could someone help me with this ? RE: Program demonstrates operations of bitwise operators without using bitwise operations - Larz60+ - Aug-18-2021 code cannot be run as presented. Please fix comments. RE: Program demonstrates operations of bitwise operators without using bitwise operations - deanhystad - Aug-18-2021 There are some errors: 1. Your program could only work if binary1 and binary2 are the same length. 2. You start by setting output to '111010000000' when it should be ''. output = '' # Output initialized to empty string output = binary1 + binary2 # Concatenate strings to it, not numbers.3. You compare strings to numbers. '1' does not equal 1. This will never be True. if binary1[i]==1 and binary2[i]==1:4. Again '1' is not 1. This would raise an exception if it was ever called because the argument for chr() must be a number, not a string. output = chr(output + str(1))5. And '0' is not 0. This would raise an exception if it was ever called because output is probably a string and you cannot add a string and an int. If output is a number this just gives you the wrong result. output = output + 0 # Append 0 when the condition is not trueI think you are better off starting over. Delete the file, grab pencil and paper and get far away from the computer. Think about how you, not the computer, would solve 1110 & 1000. Write down each step. Now apply those same steps to solving 1110 & 100. Does your algorithm work? Apply the algorithm to 1010 | 100 and to 1010 ^ 101. When it works for all cases you are ready to translate the algorithm to code. Your current program is very confused about what is a number and what is a string. In your algorithm are you treating 1 and 0 as numbers or symbols? In other words, are performing math operations with them or are you only doing comparisons (==, !=)? If you are only treating them as symbols I would keep the values as strings ('0' and '1', not 0 and 1). |