What is the xor of 0b011 and 0b101 in python?
While reviewing the solution of a problem from the Python Data Structure and Algorithm course I am taking on Udemy, I got confused at the concept of "XOR". Show
As you can see below, we started off with 2 lists- arr1 and arr2, and then the solution basically tries to find the number in arr1 that is not in arr2 (which is 5 in this example). The "XOR" appears in the line "result^=num", and I have no idea how the "result" variable changes based on value of "num".
Output:
I know for example when input 1 equals input 2 then XOR will become "0". If input 1 is 0 and input 2 is 1 OR if input 1 is 1 and input 2 is 0 then XOR will return "1". But I don't understand is what if input 1 and input 2 are not the same and they are not 1 or 0. Let's say when input 1 is 10 and input 2 is 15 OR if input 1 is 15 and input 2 is 10 then XOR will return "5". But why is "5" the XOR output in this case? Any idea to help me better understand the relationship between inputs and their XOR outputs will be greatly appreciated, hopefully in a not-so-technical manner! Bitwise operators are used for performing operations on operations on Binary pattern or Bit sequences. Python has 6 bitwise operators: AND, OR, XOR, Complement and Shift Operators. They normally operate on numbers but instead of treating them as numbers they are treated as string of bits, written in twos complement binary by the operators. Let’s go through go through a brief summary of twos compliment to make you understand better:
Binary RepresentationTwo’s Representation of Positive numbersPositive numbers have a pretty straight forward representation:
Two’s Representation of Negative numbersNegative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from “00000000” to “01111111” as the whole numbers from 0 to 127, and reserve “1xxxxxxx” for writing negative numbers. Using only 8 bits for twos-compliment negative numbers can be represented as:
Python doesn’t use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written: “…1111111111111111111011”.
BitWise Operators Table
Explanation TimeBitwise NOT/Complement OperatorThe bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones’ complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. Let’s see a few Examples:
>>> bin(3) '0b11' >>> bin(~3) '-0b100' >>> ~3 -4
Bitwise AND OperatorA bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example: >>> bin(0b110 & 0b011) # 6 & 3 '0b10' >>> 0b110 & 0b011 2 >>> 6 & 3 2
>>> 6 & 1 # Even 0 >>> 8 & 1 # Even 0 >>> 11 & 1 # Odd 1 Bitwise OR OperatorA bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example: >>> bin(0b110 | 0b011) # 6 | 3 '0b111' >>> 0b110 | 0b011 7 >>> 6 | 3 7 Bitwise XOR OperatorA bitwise XOR takes two-bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example: >>> bin(0b110 ^ 0b011) # 6 ^ 3 '0b101' >>> 0b110 ^ 0b011 5 >>> 6 ^ 3 5
Leftward Shift OperatorA leftward Shift Operator shifts the bits left y places and zeroes are shifted in to fill the discarded bits. Let’s see an image that will make you understand it better: Here the bits are shifted 1 place and a zero is filled at the end. This is same as multiplying a number by 2. See for yourself in this example: >>> 3<<1 # x< Rightward Shift OperatorA Rightward Shift Operator shifts the bits right y places and zeroes are shifted in to fill the discarded bits. Let’s see an image that will make you understand it better: Here the bits are shifted 1 place and a zero is filled at the end. This is same as //ing a number by 2. Let’s see few examples to make you understand better: >>> bin(3) '0b11' >>> bin(6) '0b110' >>> 6>>1 # '110' -> '011' 1 place shift 3 >>> 6>>2 # '110' -> '001' 2 place shift 1 >>> 6//2 3 >>> 6//4 1 Voila!! We are done. In case if you face any problem please comment below. I will be happy to help. Practice Makes you perfect!!Here are few questions which you can use to practice bitwise operators problems:
More questions are added daily with its approach and best solution. Subscribe to our site to be updated for new posts and keep learning. How is XOR calculated in Python?In Python, we can perform the bitwise XOR operation using the "^" symbol. The XOR operation can be used for different purposes; XOR of two integers, XOR of two booleans, Swapping two numbers using XOR, etc. We can also use the xor() function using the operator module in Python.
How do you do XOR of two strings in Python?How to take the bitwise XOR of two strings in Python. s1 = "abc". s2 = "a23". a_list = [chr(ord(a) ^ ord(b)) for a,b in zip(s1, s2)]. print(a_list). s3 = "". join(a_list). print(s3). What is the difference between 5 and 7 and 5 & 7 Python?So, here, 5 and 7 is the same as True and 7.
What is bitwise eXclusive or for in Python?Bitwise XOR (^) operator will take two equal length binary sequence and perform bitwise XOR operation on each pair of bit sequence. XOR operator will return 1, if both bits are different. If bits are same, it will return 0.
|