Created
May 23, 2018 22:34
-
-
Save scriptpapi/dbeac318d998fbbf16c15643189df1fe to your computer and use it in GitHub Desktop.
more bitwise manipulations
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
| // more bitwise manipulations | |
| #include <iostream> | |
| using namespace std; | |
| void set(int &num, int pos); | |
| void toggle(int &num, int pos); | |
| void unset(int &num, int pos); | |
| void cudi(); | |
| int main() { | |
| int num= 5, pos=1; | |
| //cout << "Enter a number> " << endl; | |
| //cin >> num; | |
| //cout << "Enter position shift> " << endl; | |
| //cin >> pos; | |
| // Setting a bit in the number 'num' | |
| set(num, pos); | |
| cout << "set a bet >>" << (int)(num) << endl; | |
| // Toggling a bit at nth position | |
| toggle(num, pos); | |
| cout << "toggle a bit >> " << num << endl; | |
| // Unset a bit | |
| unset(num, pos); | |
| cout << "unset a bit >>" << num << endl; | |
| cout << endl; | |
| // Crush a bit | |
| cudi(); | |
| return 0; | |
| } | |
| void set(int &num, int pos){ num |= (1 << pos); } | |
| void toggle(int &num, int pos){ num ^= (1 << pos); } | |
| void unset(int &num, int pos){ num &= (~(1 << pos));} | |
| void cudi(){ | |
| cout << "Crush a bit lil bit" << endl; | |
| cout << "Roll it up take a hit" << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment