Date post: | 01-Dec-2014 |
Category: |
Technology |
Upload: | mohamed-amin |
View: | 567 times |
Download: | 1 times |
Bitwise operations
Tuesday, July 3, 12
Logical operations
A B !A A && B A || B A ^ B
0 0 1 0 0 0
0 1 1 0 1 1
1 0 0 0 1 1
1 1 0 1 1 0
Tuesday, July 3, 12
Bitwise operations
• They operate on the bits of the arguments
• Ex:A = 1010,B = 1100
• A&B = 1000
• A|B = 1110
• A^B = 0110
• ~A = 0101
Tuesday, July 3, 12
Bitwise shift
• Shift left = multiplication by 2
• 2n = 1<<n for n ≥ 0
• Eg.
• 0010 << 1 = 0100
• 0101 << 1 = 1010
• 0011 << 3 = 1000
Tuesday, July 3, 12
Bitwise shift
• Shift right = division by 2
• Eg.
• 0010 >> 1 = 0001
• 0101 >> 1 = 0010
• 1111 >> 3 = 0001
Tuesday, July 3, 12
• To set the ith bit in A use A = A|(1<<i)
• To clear the ith bit in A use A=A&(~(1<<i))
• To check the ith bit in A use (A&(1<<i))!=0
Tuesday, July 3, 12
• k mod 2 = k & 1
• k mod 2n = k & (2n - 1)
Tuesday, July 3, 12
Representation of sets
• S = {6,5,4,3,2,1,0}
• B = {2,1,0} can be represented as 0000111
• A = {5,2} can be represented as 0100100
• A U B = 0000111 | 0100100 = 0100111
• A ∩ B = 0000111 & 0100100 = 0000100
• A - B = A & (~B) = 0000111 & 1011011 = 0000011
Tuesday, July 3, 12
• What if we want to iterate over all possible subsets of set S ? Just iterate over all binary numbers that have |S| bits i.e iterate over all binary numbers from 0 to 2|S| excluding 2|S|
Tuesday, July 3, 12
S = A[n]for(int i = 0;i<1<<n;i++)//use the ith subsetfor(int j = 0;j<n;j++)if(((1<<j)&i)!=0)//use the jth element which//exists in the ith subset
Tuesday, July 3, 12
Subset sum
• Given an array of integers of length n, can you choose some of them (at least one) such that their sum is zero?
• Constrains n<=20
Tuesday, July 3, 12
• assume the array is a set and iterate over all subsets check if there’s a subset that sums to zero
Tuesday, July 3, 12
Singleton
• Given an array of integers where each number occurs twice except one find this number
• Constrains:array size 100000
Tuesday, July 3, 12
• Use HashSet/TreeSet.
• XOR all the numbers the result is the singleton.
Tuesday, July 3, 12
EllysXors
• Given two numbers L,R compute XOR(L,L+1,L+2,…,R-2,R-1,R)
• Constrains 1≤L,R≤4,000,000,000
Tuesday, July 3, 12
Observations
• XOR(L,R) = XOR(XOR(0,…,L-1),XOR(0,z…,R))
• XOR(2k,2k+1) = 1
Tuesday, July 3, 12
Pigeonhole principle
• If n items put into m pigeonholes with n>m then at least one pigeonhole contains more than one item.
Tuesday, July 3, 12
Gray-code like
• Given an array of integers length n where the hamming distance between any two consecutive numbers is exactly 1 print Yes if the array contains four numbers such that A[i]^A[j]^A[k]^A[l]=0 and 0≤i<j<k<l<n
• 0≤A[i]<264,n<=100,000
Tuesday, July 3, 12
• for small n complete search
• for large n solution answer is always YES
Tuesday, July 3, 12
X3
• Given an array of integers length n compute the sum of A[i]^A[j] for all i,j where i<j
• Constrains n≤1,000,000
Tuesday, July 3, 12
• Think of a solution for a simpler problem then use this solution for the bigger problem.
• Simpler problem?what if the array contained only zeroes and ones the answer would be nZeroes * nOnes where nZeroes is the count of zeroes and nOnes is count of ones.
Tuesday, July 3, 12
• Similarly we can apply the same approach for each bit position so the result for ith bit is nZeroes*nOnes*1<<i where nZeroes and nOnes are the number of zeroes and ones in the ith bit.
Tuesday, July 3, 12