Created
April 15, 2015 00:16
-
-
Save mbenford/80b9e57edce82929f036 to your computer and use it in GitHub Desktop.
Bit manipulation in Javascript
This file contains 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
function setBitOn(number, bit) { | |
return number | 1 << bit; | |
} | |
function setBitOff(number, bit) { | |
return number & ~(1 << bit); | |
} | |
function toggleBit(number, bit) { | |
return number ^ 1 << bit; | |
} | |
function isBitOn(number, bit) { | |
return (number & 1 << bit) === 1 << bit; | |
} | |
function isBitOff(number, bit) { | |
return (number & 1 << bit) === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment