Created
October 25, 2022 12:08
-
-
Save AshishPandagre/a44d17709d514037dc4cc02ec1227637 to your computer and use it in GitHub Desktop.
Bit Manipulation C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int x = 10; | |
| if(x & 1) cout<<"odd"; | |
| else cout<<"even"; | |
| cout<<endl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int clearIthBit(int n, int i) { | |
| int mask = ~(1<<i); | |
| return (n&mask); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int getIthBit(int n, int i) { | |
| int mask = (1<<i); | |
| return (n & mask) > 0 ? 1 : 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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