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

Curve fitting - Lecture Notes
Curve fitting - Lecture NotesCurve fitting - Lecture Notes
Curve fitting - Lecture NotesDr. Nirav Vyas
 
eliodinsulenapier
eliodinsulenapiereliodinsulenapier
eliodinsulenapierElias Dinsa
 
Scalar product of vectors
Scalar product of vectorsScalar product of vectors
Scalar product of vectorsBed Dhakal
 
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...Dr.Summiya Parveen
 
Materi ajar phytagoras didin mahpudin
Materi ajar phytagoras didin mahpudinMateri ajar phytagoras didin mahpudin
Materi ajar phytagoras didin mahpudinDidin Mahpudin
 
Scalars & vectors
Scalars & vectorsScalars & vectors
Scalars & vectorsKhanSaif2
 
Iterative methods for the solution
Iterative methods for the solutionIterative methods for the solution
Iterative methods for the solutionOscar Mendivelso
 
The vector or cross product
The vector or cross productThe vector or cross product
The vector or cross productSabir Ahmed
 
関数(&統計の続き)(人間科学のための基礎数学)
関数(&統計の続き)(人間科学のための基礎数学)関数(&統計の続き)(人間科学のための基礎数学)
関数(&統計の続き)(人間科学のための基礎数学)Masahiro Okano
 
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.Gogely The Great
 
行列(人間科学のための基礎数学)
行列(人間科学のための基礎数学)行列(人間科学のための基礎数学)
行列(人間科学のための基礎数学)Masahiro Okano
 
Dot & cross product of vectors
Dot & cross product of vectorsDot & cross product of vectors
Dot & cross product of vectorsAshraful Tauhid
 

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

Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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 DiscoveryTrustArc
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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 FMESafe Software
 
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 educationjfdjdjcjdnsjd
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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 TerraformAndrey Devyatkin
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Recently uploaded (20)

Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

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