473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Writing a Python program that demonstrates operations of bitwise operators using binary digits. Without using the bitwise operators

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 ?
Aug 18 '21 #1
0 1513

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sandy_pt_in | last post by:
Hi C guru's can you please tell me how to do multiplication of 2 numbers using bitwize operators?? expecting reply.
12
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
4
by: sekhar_ps | last post by:
how would you print 2 powers series from 0 to 100 (like pow(2,0),pow(2,1).........pow(2,100)) with out using mathematical operators
10
by: mahi543 | last post by:
please tell me java code to add two numbers with using bitwise operators only? can we write the program in java OS is linux, java5.0 version
2
by: mahi543 | last post by:
can we write a program in java using bitwise operators to add two numbers
8
by: Aftabpasha | last post by:
Is it possible to find (integer) result of divison of a number by 7 by using Bitwise operators only? Please give a method to do so. Thank you.
14
by: Aftabpasha | last post by:
Is it possible to perform all arithmetic operations (+,-,*,/) using Bitwise operations only. Please consider signed numbers also. Thank You.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.