Created
April 26, 2016 18:51
-
-
Save dudleyjosh/df677b685e249d445702a647e7b48106 to your computer and use it in GitHub Desktop.
Test, Set, Clear & Toggle bits 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 bit_test(num, bit) { | |
return ((num >> bit) % 2 != 0) | |
} | |
function bit_set(num, bit) { | |
return num | 1 << bit; | |
} | |
function bit_clear(num, bit) { | |
return num & ~(1 << bit); | |
} | |
function bit_toggle(num, bit) { | |
return bit_test(num, bit) ? bit_clear(num, bit) : bit_set(num, bit); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment