Skip to content

Instantly share code, notes, and snippets.

@dudleyjosh
Created April 26, 2016 18:51
Show Gist options
  • Save dudleyjosh/df677b685e249d445702a647e7b48106 to your computer and use it in GitHub Desktop.
Save dudleyjosh/df677b685e249d445702a647e7b48106 to your computer and use it in GitHub Desktop.
Test, Set, Clear & Toggle bits in JavaScript
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