Scientific Calculator

(public) shiwei/dec2bin

By shiwei ryan

dec2bin(h) returns the binary representation of h as a string.

Tagged: binary decimal

var dec2bin = function(h){
var i = ceil(log2(h)+0.0000000001) - 1 ;
var j = 0;
var k = 0;
var remainder = [];
var res = "";
var quotient = 0;

while (i > 0) -> (
quotient = floor(h/2);
remainder[j] = h mod 2;
j += 1;
h = quotient;
i -= 1;
);
res = string(1);

k = len remainder - 1 ;
while( k >= 0 ) -> (
res = concat(res,string(remainder[k]));
k -= 1;
);
res;
}

spam? | offensive?

0 Comments

Sign in to leave a comment