Operator | Use Case | Explanation | Example | Status |
---|---|---|---|---|
AND | a & b |
if a == b == 1, then a & b = 1, otherwise = 0 | 1 & 1 = 1 , 1 & 0 = 0 ,0 & 1 = 0 , 0 & 0 = 0 |
OK |
OR | a | b |
if a == 1 or b == 1, then a | b = 1 , otherwise = 0 |
1 | 1 = 1 , 1 | 0 = 1 ,0 | 1 = 1 , 0 | 0 = 0 |
OK |
XOR | a ^ b |
if (a,b) = (1,0) or (0,1), then a ^ b = 1, otherwise = 0 | 1 ^ 1 = 0 , 1 ^ 0 = 1 ,0 ^ 1 = 1 , 0 ^ 0 = 0 |
OK |
NOT | - a |
inverse | AGAIN | |
LEFT MOVE | a << b |
left shift a ( in binary) b times |
9 << 2 = 36 |
OK |
RIGHT MOVE | a >> b |
right shift a ( in binary) b times |
9 >> 2 = 2 , -9 >> 2 = -3 |
AGAIN |
RIGHT MOVE AND ADD 0 | a >>> b |
right shift a ( in binary) b times, remove out-of-boundry bit, and add 0 left |
19 >>> 2 = 4 |
AGAIN |
- XOR
-
Taking XOR of a number with itself returns 0, e.g.,
- 1 ^ 1 = 0
- 29 ^ 29 = 0
-
Taking XOR of a number with 0 returns the same number, e.g.,
- 1 ^ 0 = 1
- 31 ^ 0 = 31
-
XOR is Associative & Commutative, which means:
- (a ^ b) ^ c = a ^ (b ^ c)
- a ^ b = b ^ a
-
15 & 9 = 9
(1111 & 1001 = 1001) (bin(15) = 1111, bin(9) = 1001)15 | 9 = 15
(1111 | 1001 = 1111) (bin(15) = 1111, bin(9) = 1001)15 ^ 9 = 6
(1111 ^ 1001 = 0110) (bin(15) = 1111, bin(9) = 1001)9 << 2 = 36
( bin(9) = 1001, left shift 2 times -> 100100 -> int('100100', 2) = 36)9 >> 2 = 2
( bin(9) = 1001, right shift 2 times -> 10 -> int('10', 2) = 2)-9 >> 2 = -3
19 >>> 2 = 4
( bin(19) = 10001, right shift 2 times -> 100 -> add0
make the length same as origin -> 00100 -> bin('100',2 )= 4)- Minus :
0 -> 1
,0 -> 1
thenadd 1
- (9)
: - (1001) -> (0110) + 1 ->0111
- (38)
: - (00100110) -> (11011001) + 1 ->11011010
# binary 1 = 01
# binary 2 = 10
# so -> 01 XOR 10 = 11 -> 3
1 ^ 2 = 3
# binary 1 = 01
# binary 3 = 11
# so -> 01 XOR 11 = 10 -> 2
1 ^ 3 = 2
# binary 1 = 01
# binary 4 = 100
# so -> 01 XOR 100 = 101 -> 5
1 ^ 4 = 5
- Calculate XOR from 1 to n ?
Input : n = 6
Output : 7
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7
Input : n = 7
Output : 0
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 0
# Python 3 Program to find
# XOR of numbers from 1 to n.
# Function to calculate xor
def computeXOR(n) :
# Modulus operator are expensive
# on most of the computers. n & 3
# will be equivalent to n % 4.
# if n is multiple of 4
if n % 4 == 0 :
return n
# If n % 4 gives remainder 1
if n % 4 == 1 :
return 1
# If n%4 gives remainder 2
if n % 4 == 2 :
return n + 1
# If n%4 gives remainder 3
return 0
# Driver Code
if __name__ == "__main__" :
n = 5
# function calling
print(computeXOR(n))
# This code is contributed by ANKITRAI1
- Given an array of n-1n−1 integers in the range from 11 to nn, find the one number that is missing from the array ?
def find_missing_number(arr):
n = len(arr) + 1
# x1 represents XOR of all values from 1 to n
x1 = 1
for i in range(2, n+1):
x1 = x1 ^ i
# x2 represents XOR of all values in arr
x2 = arr[0]
for i in range(1, n-1):
x2 = x2 ^ arr[i]
# missing number is the xor of x1 and x2
return x1 ^ x2
def main():
arr = [1, 5, 2, 6, 4]
print('Missing number is:' + str(find_missing_number(arr)))
main()
- https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Obsolete_Pages/Obsolete_Pages/Obsolete_Pages/%E9%81%8B%E7%AE%97%E5%AD%90/%E4%BD%8D%E5%85%83%E9%81%8B%E7%AE%97%E5%AD%90
- http://140.129.118.16/~richwang/99-2-Courses/About_C_BitOperation.pdf
- https://github.com/labuladong/fucking-algorithm/blob/master/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E5%B8%B8%E7%94%A8%E7%9A%84%E4%BD%8D%E6%93%8D%E4%BD%9C.md