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

4/17/2019 3:27:01 AM

Jon Jesh

+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/

+19

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

+15

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

+10

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

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

+9

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

+8

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

+8

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

+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

+1

+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

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

+1

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

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

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

-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

-2

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

-3

Okay, im sorry.. Im an beginner actually

-3

Okay, thankyou bro