Created
October 14, 2014 16:00
-
-
Save nicoptere/4551c62b798ac606d026 to your computer and use it in GitHub Desktop.
bit masking, basic operations
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
/** | |
* checks if a value has a given bit set to 1 | |
* @value the int / uint to test | |
* @mask the bits to check | |
**/ | |
function bit_isset( value, mask ) | |
{ | |
return ( value & mask ) != 0; | |
} | |
/** | |
* sets a bit mask to a int / uint | |
* @value the int / uint to set the bit mask onto | |
* @mask the bits to set | |
**/ | |
function bit_set( value, mask ) | |
{ | |
value |= mask; | |
return value; | |
} | |
/** | |
* clears a bit mask | |
* @value the int / uint to clear | |
* @mask the bits to clear | |
**/ | |
function bit_clear( value, mask ) | |
{ | |
value &= ~mask; | |
return value; | |
} | |
/** | |
* toggles a bit mask ( 1s become 0s & 0s become 1s ) | |
* @value the int / uint to toggle | |
* @mask the bits to toggle | |
**/ | |
function bit_toggle( value, mask ) | |
{ | |
value ^= mask; | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment