+ All Categories
Home > Technology > Bit-wise Operation Slides

Bit-wise Operation Slides

Date post: 01-Dec-2014
Category:
Upload: mohamed-amin
View: 567 times
Download: 1 times
Share this document with a friend
Description:
 
22
Bitwise operations Tuesday, July 3, 12
Transcript
Page 1: Bit-wise Operation Slides

Bitwise operations

Tuesday, July 3, 12

Page 2: Bit-wise Operation Slides

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

Page 3: Bit-wise Operation Slides

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

Page 4: Bit-wise Operation Slides

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

Page 5: Bit-wise Operation Slides

Bitwise shift

• Shift right = division by 2

• Eg.

• 0010 >> 1 = 0001

• 0101 >> 1 = 0010

• 1111 >> 3 = 0001

Tuesday, July 3, 12

Page 6: Bit-wise Operation Slides

• 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

Page 7: Bit-wise Operation Slides

• k mod 2 = k & 1

• k mod 2n = k & (2n - 1)

Tuesday, July 3, 12

Page 8: Bit-wise Operation Slides

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

Page 9: Bit-wise Operation Slides

• 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

Page 10: Bit-wise Operation Slides

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

Page 11: Bit-wise Operation Slides

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

Page 12: Bit-wise Operation Slides

• 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

Page 13: Bit-wise Operation Slides

Singleton

• Given an array of integers where each number occurs twice except one find this number

• Constrains:array size 100000

Tuesday, July 3, 12

Page 14: Bit-wise Operation Slides

• Use HashSet/TreeSet.

• XOR all the numbers the result is the singleton.

Tuesday, July 3, 12

Page 15: Bit-wise Operation Slides

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

Page 16: Bit-wise Operation Slides

Observations

• XOR(L,R) = XOR(XOR(0,…,L-1),XOR(0,z…,R))

• XOR(2k,2k+1) = 1

Tuesday, July 3, 12

Page 17: Bit-wise Operation Slides

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

Page 18: Bit-wise Operation Slides

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

Page 19: Bit-wise Operation Slides

• for small n complete search

• for large n solution answer is always YES

Tuesday, July 3, 12

Page 20: Bit-wise Operation Slides

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

Page 21: Bit-wise Operation Slides

• 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

Page 22: Bit-wise Operation Slides

• 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


Recommended