Python quiz, don't understand question

What is the output of this code? Print (5|3) Please help me understand why this is 7? I've tested this code using different numbers and I still can't figure out the logic... If you also thought this was hard to understand please EDIT: DO NOT give my question a like as asking for such things will create negativity in a rather positive community environment...

4/17/2019 3:27:01 AM

Jon Jesh

22 Answers

New Answer


Jon Jesh It's the bitwise OR operator. 5 | 3 results in 7 because 0 0 0 0 0 1 0 1 | 0 0 0 0 0 0 1 1 - - - - - - - - - 0 0 0 0 0 1 1 1 = 7 https://www.sololearn.com/learn/4073/


Here's an introduction to bitwise operators. https://www.sololearn.com/learn/4070/


To those who are downvoting: why do it in the first place? There's no valid reason to downvote Jon's question and answers.


Muhammad Arbi 1: you should create your own post, not ask on someone else's. 2: we need more information like where is your code? What is the error?


Diego thank you so much. I guess i need to learn bitwise with my python skills. Very, very helpful good sir


Every post on this thread has 3+ downvotes. Boy am I glad we're supposed to be a positive community, lol!


In the future, I would recommend not asking for likes/up votes in your question. That is a surefire way to get down votes.


You can search bitwise Boolean logic. Here is the whole thing in a nutshell https://www.advanced-ict.info/mathematics/bitwise.html


print(5 | 3) output 7 To fully understand this you need to know Boolean logic, along with binary math. Hang in there, don't give up. 👍


i dont understand why the hostilaty. This was a question in a challenge that both of us got wrong. I tries googling it and tried using the code editor and still dont understand. any help would be very appreciates


Mr. listen. "Mr." i googled and did research for 10-15 minutes. please, do tell me what exactly you would "google" to figure out that "print (5|3)" had anything to do with bitwise... your answer was unhelpful and condicending.


It's important to know your logic truth tables. https://code.sololearn.com/cMpDcIY1S7lY/?ref=app And how to write code that explains itself. https://code.sololearn.com/cbUZQvD9le8A/?ref=app


The | operator is called as the BITWISE OR operator. 0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1 Here's how it works: 5 = 00000101 (8 bit binary value of 5) 3 = 00000011 (8 bit binary value of 3) ------------------------------------ 5|3 = 00000111 = 7 (in decimal) Every bit of the two numbers are operated with | (OR) operator.


κεvιπ lεvιπ thank you, very well explained and helpful!


Muhammad Arbi I recommend just focusing on the python course that you have already started itself. There is enough there to learn, just keep in mind that bitwise operators exists and can executed in python scripts. Keep it up, this is a good place to start


Shivam you are best asking your question in a new Q & A. They prefer you to not just add it to someone elses post


For to understanding you must to learning a struct of byte in binary system. The number 5 in binary is 00000101 And number 3 in binary is 00000011 Logical | using to binary number The result of | is. 00000111 This is the number 7


Why every i try to run my code, there was an error?


Okay, im sorry.. Im an beginner actually


Okay, thankyou bro