Nodejs Number Calculate twos(n)

Here you can find the source of twos(n)

Method Source Code

/* Extend the Number prototype with a function called "twos" that accepts one parameter (n), and when called, returns the two's-complement representation of the number in "n" bits in a string. (first bit is sign, rest is normal for positive, for negative counts up from maximum neg number expressible with those bits)

(-2).twos(3) == "110";/* ww  w.  j a v a  2 s  .  c  o m*/
(8).twos(5) == "01000";
(-8).twos(5) == "11000";
(-16).twos(5) == "10000"; */

// my solution (which does the job but doesnt use bitwise operators)

Number.prototype.twos = function(n) {
  if (this >= 0) {return pad(this.toString(2),n)}
  // if eg n = 5, 4 bits can represent up to 2**4 => -16, which is represented by "10000"  (or (16).toString(2))
  var max = Math.pow(2,n-1);
  // to get some other number, -x, take (max - x) and represent it in n-1 binary digits and append it to a leading 1
  return "1" + pad((max + this).toString(2),n-1)
}

function pad(num, size) {
    var s = "00000000000000000000" + num;
    return s.substr(s.length-size);
}

// the official solution

Number.prototype.twos = function(n) {
  var ret = "";
  while(n)
  {
    ret += ( (this >> --n) & (1) );
  }
  return ret;
};

Related

  1. succ()
    Number.prototype.succ = function() {
        return this + 1;
    };
    
  2. tan()
    Number.prototype.tan = function() {
        return Math.tan(this);
    
  3. testNumber()
    function Number(){
      this.start = 20
      this.testNumber();
      this.answer = 1
    };
    Number.prototype.testNumber = function(){
      var i = 1;
      var start = this.start;
      while(i <= 20){
    ...
    
  4. trim(a, b)
    Number.prototype.trim = function(a, b) {
      var min = Math.min(a, b),
          max = Math.max(a, b);
      return Math.min(Math.max(parseInt(this, 10), min), max);
    };
    
  5. tween(target, position)
    Number.prototype.tween = function(target, position){
      return this + (target-this) * position;
    };
    
  6. valueOf() return x; };
    function foo(a, b) {
        var result = a + b;
        if (result)
            return (a + b) + result + this;
        else
            return this.f;
    noInline(foo);
    var x;
    ...
    
  7. with_sign()
    Number.prototype.with_sign = function(){
      return ( this > 0 ) ? "+"+this : this.toString();