Last active
December 14, 2017 06:19
-
-
Save behcet/d5a21e552f1e0dc10f252c3d59acfbdc to your computer and use it in GitHub Desktop.
javascript bitwise not operator
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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators | |
function createBinaryString (nMask) { | |
// nMask must be between -2147483648 and 2147483647 | |
for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag < 32; | |
nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1); | |
return sMask; | |
} | |
[-2, -1, 0, 1, 2].forEach(number => { | |
console.log(number + ' \t\t\t\t\t==>\t ' + ~number + ' \n' + | |
createBinaryString(number) + ' \t--->\t ' + createBinaryString(~number) | |
+ '\n' + '-'.repeat(80)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment