Skip to content

Instantly share code, notes, and snippets.

@hisaos
Created September 5, 2012 05:04
Show Gist options
  • Save hisaos/3630838 to your computer and use it in GitHub Desktop.
Save hisaos/3630838 to your computer and use it in GitHub Desktop.
deg2binTable
function deg2binTable(num, pow) {
var result = [];
var maxNum = Math.pow(2, pow - 1);
for(var i = 0; i <= num; i++)
{
var target = i;
var nesNum = maxNum;
var packing = "";
var str = "";
while(target < nesNum) { packing += "0"; nesNum >>= 1; }
while(target > 0)
{
str += "" + (target & 1);
target >>= 1;
}
str += packing;
result.push(str.reverse(str));
}
return result;
}
String.prototype.reverse = function(str)
{
result = "";
for(var i = str.length - 1; i >= 0; i--)
{
result += str[i];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment