Skip to content

Instantly share code, notes, and snippets.

@AshishPandagre
Created October 25, 2022 12:08
Show Gist options
  • Save AshishPandagre/a44d17709d514037dc4cc02ec1227637 to your computer and use it in GitHub Desktop.
Save AshishPandagre/a44d17709d514037dc4cc02ec1227637 to your computer and use it in GitHub Desktop.
Bit Manipulation C++
int x = 10;
if(x & 1) cout<<"odd";
else cout<<"even";
cout<<endl;
int clearIthBit(int n, int i) {
int mask = ~(1<<i);
return (n&mask);
}
int getIthBit(int n, int i) {
int mask = (1<<i);
return (n & mask) > 0 ? 1 : 0;
}
int setIthBit(int n, int i) {
int mask = (1<<i);
return (n | mask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment