Skip to content

Instantly share code, notes, and snippets.

@scriptpapi
Created May 23, 2018 22:34
Show Gist options
  • Select an option

  • Save scriptpapi/dbeac318d998fbbf16c15643189df1fe to your computer and use it in GitHub Desktop.

Select an option

Save scriptpapi/dbeac318d998fbbf16c15643189df1fe to your computer and use it in GitHub Desktop.
more bitwise manipulations
// 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