Created
December 1, 2016 19:07
-
-
Save mgronhol/8b22048c35539cd51eb6e54b52510e1c to your computer and use it in GitHub Desktop.
Some simple bit handling
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
| // 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