Created
August 12, 2013 17:25
-
-
Save jussiry/6213050 to your computer and use it in GitHub Desktop.
Learn bitwise operations with this helper function
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
window.printbit = function(){ | |
var saBits = [], | |
naLengths = []; | |
for (var i=0; i<arguments.length; i++) { | |
var arg = arguments[i]; | |
if (typeof arg == 'string') { | |
saBits.push(eval(arguments[i-2]+arg+arguments[i-1]).toString(2)); | |
arguments[i] = arguments[i-2]+arguments[i]+arguments[i-1]; | |
} else { | |
saBits.push(arg.toString(2)); | |
} | |
naLengths.push(arguments[i].toString().length + saBits[i].length); | |
} | |
var nLongest = Math.max.apply(null, naLengths); | |
for (var i=0; i<arguments.length; i++) { | |
console.log(arguments[i] + " ".repeat(nLongest-saBits[i].length-arguments[i].toString().length+2) + saBits[i]); | |
} | |
} | |
String.prototype.repeat = function (a){var b="",c=this;if(a<1)return"";for(;a;){if(a&1)b+=c;if(a>>=1)c+=c}return b} |
Author
jussiry
commented
Aug 12, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment