Skip to content

Instantly share code, notes, and snippets.

@cdelahousse
Created June 4, 2012 05:14
Show Gist options
  • Save cdelahousse/2866485 to your computer and use it in GitHub Desktop.
Save cdelahousse/2866485 to your computer and use it in GitHub Desktop.
Third, overly clever, reimplementation of a binary coded clock
function pad(str,num) { return str.length < num ? pad('0' + str,num) : str; }
function dec2bin(num) {
return num <= 1 ? '' + num : dec2bin(Math.floor(num>>1)) + '' + (num & 1); // n % d == n & (d-1) if d is a power of two
}
function binaryClock(d) {
var binLen = 4, //Binary digit length including paddying
bin = (pad('' + d.getHours(),2)
+ pad('' + d.getMinutes(),2)
+ pad('' + d.getSeconds(),2) ).split('') //string of time digits to array
.map(function (n) { return pad(dec2bin(+n), binLen).split(''); }); //Conv and pad digits
return bin[0].map(function (_,i) { //Zip
return bin.map(function(d) {return d[i]; }).join(''); //join elems that share index
}).join("\n");
}
console.log(binaryClock(new Date()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment