Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Created December 1, 2016 19:07
Show Gist options
  • Select an option

  • Save mgronhol/8b22048c35539cd51eb6e54b52510e1c to your computer and use it in GitHub Desktop.

Select an option

Save mgronhol/8b22048c35539cd51eb6e54b52510e1c to your computer and use it in GitHub Desktop.
Some simple bit handling
// options
final int OPTION1 = 0;
final int OPTION2 = 1;
final int OPTION3 = 2;
// ad nauseam ...
// usage: if( getBit( flags, OPTION3 ) ){ ....
boolean getBit( int value, int pos ){
if( (value & ( 1 << pos ) ) != 0 ){ return true; }
else { return false; }
}
int setBit( int original, int pos, boolean value ){
if( value ){
return original | (1 << pos);
}
else {
return original & (~( 1 << pos ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment