15 AnswersNew Answer
Mr. 12, Learn the binary system and all these questions will disappear. You don't know how to convert numbers, that's the problem. >> and <<: These are byte shifts. That is, if you do 2 << 2, then this is how it works: 2 in binary = 10 We move the number two bytes to the left, and we get 1000, and 1000 (bin) = 8. To understand how bytes are arranged, also study the binary system and see how numbers are stored in computer memory, you will understand everything, how to move bytes too | : joins two sets, removing duplicates & : shows identical elements of sets - : shows which elements are not from, the first element in the second. ^ : shows which elements of two arrays are only in them, that is, not common elements (opposite to &)
Granger Great explanation I didn't know about the byte shift
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
Mr. 12 Go through this section to see how set operators work https://www.sololearn.com/learn/Python/2464
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))
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
Rik Wittkopp, but now you know :)
Mr. 12, Learn the structure of bytes. There are 8 digits, and, depending on what number, the corresponding digits will be. Learn it. This is not a small topic, you will have to write a lot, and I am not good at English, and the translator does not translate correctly)
Rik Wittkopp what if one contain 01 and another contain 0011?
Rik Wittkopp bro, i didn't understand print (bin(2)) =01b0 how and why?
Rik Wittkopp but how bro 0b11 is 3, and 0b101 is 5 but how it becomes 7? and also what will &, |, <<, >>, ^ will do?
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.
Granger so, Shifting means just adding zeors to left of right?
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
Mr. 12 0 => 0 0 => 0 1 0 => 1 1 1 => 1 Result => 0b11 Same rules apply