Python Forum
Program demonstrates operations of bitwise operators without using bitwise operations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program demonstrates operations of bitwise operators without using bitwise operations
#1
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: 1000
Hi 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 ?
Reply
#2
code cannot be run as presented.
Please fix comments.
Reply
#3
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 true
I 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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create bot to automate operations in IQ Option JonatasCavalini 1 31,465 Jul-25-2025, 11:26 PM
Last Post: liberwicca
  Use of if - and operators Pedro_Castillo 1 1,494 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Mixing Boolean and comparison operators Mark17 3 3,536 Jul-11-2022, 02:20 AM
Last Post: perfringo
  Replicate Excel operations with Python Lumberjack 3 3,527 May-10-2022, 01:44 AM
Last Post: Lumberjack
Shocked An array "mystery": The same array, the same operations but different outcomes mewss 3 3,593 Feb-17-2021, 06:34 PM
Last Post: mewss
  Bitwise ~ Operator muzikman 10 7,201 Feb-07-2021, 04:07 PM
Last Post: muzikman
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 3,846 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  DataFrame operations didn't change orginal HoldYourBreath 5 4,383 Jan-10-2021, 02:01 PM
Last Post: jefsummers
  Matrix Operations Without Numpy or Incorporating Python into Webpage ebryski 1 4,766 Nov-26-2020, 12:50 PM
Last Post: jefsummers
  Bitwise Slicing Peter_Truman 6 6,682 Aug-25-2020, 06:41 AM
Last Post: Peter_Truman

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.