Nodejs Number to Binary Convert bin32()

Here you can find the source of bin32()

Method Source Code

/**/*  w  w w. java2s . c  o m*/
 * @param {number} n - a positive integer
 * @return {number} - a positive integer
 */

Number.prototype.bin32 = function () {
   var result = this.toString(2);
   while (result.length < 32) {
      result = '0' + result;
   }
   return result;
}

var reverseBits = function (n) {
   return parseInt(n.bin32().split('').reverse().join(''), 2);
};

Related

  1. bin()
    Number.prototype.bin = function () { 
      var shifted = this
        , result = '';
      for (var flag = 0; flag < 32; flag++) {
        result += String(shifted >>> 31);
        shifted <<= 1;
      return result;