Skip to content

Instantly share code, notes, and snippets.

@mattneary
Created November 19, 2011 18:17
Show Gist options
  • Save mattneary/1379160 to your computer and use it in GitHub Desktop.
Save mattneary/1379160 to your computer and use it in GitHub Desktop.
Decimal to Binary
function(
n, //Number
i,a //PLaceholders
){
for( //Loop
a=[i=0]; //Initialize (Avoid comma in assignment)
a[i]=n>>i&1, //Save bit
n>>i++; //Check if there are more bits and increment
);
return a
}
function(n,i,a){for(a=[i=0];a[i]=n>>i&1,n>>i++;);return a}
{
"name": "Decimal to Binary",
"description": "Converts decimal number into an array of bits",
"contributors": [
"mattneary"
],
"keywords": [
"binary",
"bitwise"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment