May 1st, 2007

JavaScript: Fun with the Left Shift Operator

  Number.prototype.toHex = function() {
    var hex = "0123456789ABCDEF";
    return hex.substr((this >> 4) & 0x0F, 1) + hex.substr(this & 0x0F,1);
  }
(255).toHex(); //ff

[255, 255, 255].invoke('toColorPart').join(''); //ffffff For the Prototype folk