Python quiz, don't understand question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

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...

17th Apr 2019, 3:27 AM
Jon Jesh
Jon Jesh - avatar
17 Answers
+ 50
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/
17th Apr 2019, 3:44 AM
Diego
Diego - avatar
+ 20
You can search bitwise Boolean logic. Here is the whole thing in a nutshell https://www.advanced-ict.info/mathematics/bitwise.html
2nd May 2019, 3:33 PM
ron
+ 15
Here's an introduction to bitwise operators. https://www.sololearn.com/learn/4070/
17th Apr 2019, 3:54 AM
Diego
Diego - avatar
+ 10
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. 👍
17th Apr 2019, 10:08 PM
ron
+ 9
To those who are downvoting: why do it in the first place? There's no valid reason to downvote Jon's question and answers.
17th Apr 2019, 3:44 AM
Diego
Diego - avatar
+ 8
Diego thank you so much. I guess i need to learn bitwise with my python skills. Very, very helpful good sir
17th Apr 2019, 3:52 AM
Jon Jesh
Jon Jesh - avatar
+ 2
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
17th Apr 2019, 3:35 AM
Jon Jesh
Jon Jesh - avatar
+ 1
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.
17th Apr 2019, 4:26 AM
Jon Jesh
Jon Jesh - avatar
+ 1
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
18th Apr 2019, 3:06 AM
Louis
Louis - avatar
+ 1
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.
19th Apr 2019, 1:48 AM
Nafis
Nafis - avatar
+ 1
κεvιπ lεvιπ thank you, very well explained and helpful!
19th Apr 2019, 12:48 PM
Jon Jesh
Jon Jesh - avatar
0
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
17th Apr 2019, 7:04 PM
Jon Jesh
Jon Jesh - avatar
0
Shivam you are best asking your question in a new Q & A. They prefer you to not just add it to someone elses post
19th Apr 2019, 12:47 PM
Jon Jesh
Jon Jesh - avatar
- 1
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
17th Apr 2019, 2:00 PM
vitalii
vitalii - avatar
- 2
Why every i try to run my code, there was an error?
17th Apr 2019, 6:44 PM
Bee.
Bee. - avatar
- 3
Okay, im sorry.. Im an beginner actually
17th Apr 2019, 6:58 PM
Bee.
Bee. - avatar
- 3
Okay, thankyou bro
17th Apr 2019, 7:06 PM
Bee.
Bee. - avatar