Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created September 22, 2013 07:52
Show Gist options
  • Save mryoshio/6657746 to your computer and use it in GitHub Desktop.
Save mryoshio/6657746 to your computer and use it in GitHub Desktop.
basic calculations with bit operation
#include <iostream>
#define df(f) cout << "# " << f << endl
using namespace std;
void get_bit(int num, int i) {
df(__func__ << "(" << num << ", " << i << ")");
cout << "Get " << i << "'th bit is : ";
cout << ((num & (1 << i)) != 0) << endl;
}
void set_bit(int num, int i) {
df(__func__ << "(" << num << ", " << i << ")");
cout << "Set " << i << "'th bit => ";
cout << (num | (1 << i)) << endl;
}
void clear_bit(int num, int i) {
df(__func__ << "(" << num << ", " << i << ")");
int mask = ~(1 << i);
cout << "Clear " << i << "'th bit => ";
cout << (num & mask) << endl;
}
int main() {
get_bit(5, 1);
set_bit(5, 1);
clear_bit(5, 2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment