can anyone explain me this arthimatic operations | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
8th Aug 2020, 6:20 AM
Mr. 12
Mr. 12 - avatar
12 Answers
+ 3
Granger Great explanation I didn't know about the byte shift
8th Aug 2020, 7:56 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Mr. 12 When using the | operator, you are comparing the 2 sets against each other. Overlapping them in a way. So when you overlap 0b1100 and 0b110, the following occurs as you look at the numbers from the left. 1 => 1 1 1 => 1 0 1 => 1 0 0 => 0 Result is 0b1110 Remember that 1 is true & 0 is false, so if given an option to choose between true and false, the code will return true print(2|3) #3 print(0b10|0b11) #3 =>0b11
8th Aug 2020, 11:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Mr. 12 Go through this section to see how set operators work https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2464
8th Aug 2020, 7:20 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
I have seen something like this before which was explained as set operators working on the binary format of the numbers. The following snippet may help you understand more print(3|5) print(bin(3)) print(bin(4)) print(bin(5))
8th Aug 2020, 6:27 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Mr. 12 Ahh, the joys of binary numbers. As you discovered, 0b10 is the binary equivilent of 2. Therefore: 0b11 is 3 0b100 is 4 0b101 is 5 print(0b11|0b101) is 0b111 0b111 is 7
8th Aug 2020, 6:39 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp what if one contain 01 and another contain 0011?
8th Aug 2020, 12:30 PM
Mr. 12
Mr. 12 - avatar
0
Rik Wittkopp bro, i didn't understand print (bin(2)) =01b0 how and why?
8th Aug 2020, 6:30 AM
Mr. 12
Mr. 12 - avatar
0
Rik Wittkopp but how bro 0b11 is 3, and 0b101 is 5 but how it becomes 7? and also what will &, |, <<, >>, ^ will do?
8th Aug 2020, 6:48 AM
Mr. 12
Mr. 12 - avatar
0
Granger thank you bro, i know about binary number but i confused after it is printing 0b11 for 3,which is only 11 or 011 or 0011 for 3 in binary. i just confused with 'b' presenting in 0b11. And also thank your for explaining the arthimatic operators, Now I Got It.
8th Aug 2020, 7:57 AM
Mr. 12
Mr. 12 - avatar
0
Granger so, Shifting means just adding zeors to left of right?
8th Aug 2020, 8:00 AM
Mr. 12
Mr. 12 - avatar
0
Rik Wittkopp can u explain me more about this? if we join 2 | 3 (0b10 | 0b11) ans would be 0b10 right? because 1 is already there in 0b10? Mr. 12 Ahh, the joys of binary numbers. As you discovered, 0b10 is the binary equivilent of 2. Therefore: 0b11 is 3 0b100 is 4 0b101 is 5 print(0b11|0b101) is 0b111 0b111 is 7
8th Aug 2020, 9:57 AM
Mr. 12
Mr. 12 - avatar
0
Mr. 12 0 => 0 0 => 0 1 0 => 1 1 1 => 1 Result => 0b11 Same rules apply
8th Aug 2020, 12:36 PM
Rik Wittkopp
Rik Wittkopp - avatar