SlideShare a Scribd company logo
1 of 22
Download to read offline
Bitwise operations

Tuesday, July 3, 12
Logical operations
A

!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

Tuesday, July 3, 12

B

1

0

1

1

0
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
n = 1<<n for n ≥ 0
•2
• 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 clear the i bit in A use A=A&(~(1<<i))
• To check the i bit in A use (A&(1<<i))!=0
To set the ith bit in A use A = A|(1<<i)
th

th

Tuesday, July 3, 12
• k mod 2 = k & 1
n = k & (2n - 1)
• k mod 2

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
|S| excluding
all binary numbers from 0 to 2
|S|
2

Tuesday, July 3, 12
S = A[n]
for(int i = 0;i<1<<n;i++)
//use the ith subset
for(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

•
Tuesday, July 3, 12

0≤A[i]<264,n<=100,000
• 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

th bit
i

for each bit position so the result for
is nZeroes*nOnes*1<<i where nZeroes
and nOnes are the number of zeroes and
ones in the ith bit.

Tuesday, July 3, 12

More Related Content

What's hot

eliodinsulenapier
eliodinsulenapiereliodinsulenapier
eliodinsulenapier
Elias Dinsa
 
Scalar product of vectors
Scalar product of vectorsScalar product of vectors
Scalar product of vectors
Bed Dhakal
 
Iterative methods for the solution
Iterative methods for the solutionIterative methods for the solution
Iterative methods for the solution
Oscar Mendivelso
 
The vector or cross product
The vector or cross productThe vector or cross product
The vector or cross product
Sabir Ahmed
 

What's hot (18)

Curve fitting
Curve fittingCurve fitting
Curve fitting
 
Curve fitting - Lecture Notes
Curve fitting - Lecture NotesCurve fitting - Lecture Notes
Curve fitting - Lecture Notes
 
eliodinsulenapier
eliodinsulenapiereliodinsulenapier
eliodinsulenapier
 
Scalar product of vectors
Scalar product of vectorsScalar product of vectors
Scalar product of vectors
 
Data Approximation in Mathematical Modelling Regression Analysis and Curve Fi...
Data Approximation in Mathematical Modelling Regression Analysis and Curve Fi...Data Approximation in Mathematical Modelling Regression Analysis and Curve Fi...
Data Approximation in Mathematical Modelling Regression Analysis and Curve Fi...
 
Materi ajar phytagoras didin mahpudin
Materi ajar phytagoras didin mahpudinMateri ajar phytagoras didin mahpudin
Materi ajar phytagoras didin mahpudin
 
Curve Fitting
Curve FittingCurve Fitting
Curve Fitting
 
Unit 1
Unit 1Unit 1
Unit 1
 
Scalars & vectors
Scalars & vectorsScalars & vectors
Scalars & vectors
 
Teori bilangan (14 - 19)
Teori bilangan (14 - 19)Teori bilangan (14 - 19)
Teori bilangan (14 - 19)
 
Iterative methods for the solution
Iterative methods for the solutionIterative methods for the solution
Iterative methods for the solution
 
The vector or cross product
The vector or cross productThe vector or cross product
The vector or cross product
 
Ch 2 ~ vector
Ch 2 ~ vectorCh 2 ~ vector
Ch 2 ~ vector
 
関数(&統計の続き)(人間科学のための基礎数学)
関数(&統計の続き)(人間科学のための基礎数学)関数(&統計の続き)(人間科学のための基礎数学)
関数(&統計の続き)(人間科学のための基礎数学)
 
1 ESO - Unit 04 - Exercises 1.4.4. - Powers and Square Roots.
1 ESO - Unit 04 - Exercises 1.4.4. - Powers and Square Roots.1 ESO - Unit 04 - Exercises 1.4.4. - Powers and Square Roots.
1 ESO - Unit 04 - Exercises 1.4.4. - Powers and Square Roots.
 
行列(人間科学のための基礎数学)
行列(人間科学のための基礎数学)行列(人間科学のための基礎数学)
行列(人間科学のための基礎数学)
 
Matrices
MatricesMatrices
Matrices
 
Dot & cross product of vectors
Dot & cross product of vectorsDot & cross product of vectors
Dot & cross product of vectors
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Bit-wise Operation Slides

  • 2. Logical operations A !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 Tuesday, July 3, 12 B 1 0 1 1 0
  • 3. 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
  • 4. Bitwise shift • Shift left = multiplication by 2 n = 1<<n for n ≥ 0 •2 • Eg. • 0010 << 1 = 0100 • 0101 << 1 = 1010 • 0011 << 3 = 1000 Tuesday, July 3, 12
  • 5. Bitwise shift • Shift right = division by 2 • Eg. • 0010 >> 1 = 0001 • 0101 >> 1 = 0010 • 1111 >> 3 = 0001 Tuesday, July 3, 12
  • 6. • • To clear the i bit in A use A=A&(~(1<<i)) • To check the i bit in A use (A&(1<<i))!=0 To set the ith bit in A use A = A|(1<<i) th th Tuesday, July 3, 12
  • 7. • k mod 2 = k & 1 n = k & (2n - 1) • k mod 2 Tuesday, July 3, 12
  • 8. 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
  • 9. • 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 |S| excluding all binary numbers from 0 to 2 |S| 2 Tuesday, July 3, 12
  • 10. S = A[n] for(int i = 0;i<1<<n;i++) //use the ith subset for(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
  • 11. 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
  • 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
  • 13. Singleton • Given an array of integers where each number occurs twice except one find this number • Constrains:array size 100000 Tuesday, July 3, 12
  • 14. • Use HashSet/TreeSet. • XOR all the numbers the result is the singleton. Tuesday, July 3, 12
  • 15. 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
  • 16. Observations • XOR(L,R) = XOR(XOR(0, …,L-1),XOR(0,z…,R)) • XOR(2k,2k+1) = 1 Tuesday, July 3, 12
  • 17. 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
  • 18. 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 • Tuesday, July 3, 12 0≤A[i]<264,n<=100,000
  • 19. • for small n complete search • for large n solution answer is always YES Tuesday, July 3, 12
  • 20. 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
  • 21. • 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
  • 22. • Similarly we can apply the same approach th bit i for each bit position so the result for is nZeroes*nOnes*1<<i where nZeroes and nOnes are the number of zeroes and ones in the ith bit. Tuesday, July 3, 12