Skip to content Skip to sidebar Skip to footer

Is There A Formula For Calculating Or Estimating The Number Of Digits Of A Binary Integer?

ex : base 10(999) => 3 digits === base 2(1111100111) => 10 digits I am currently using a table to make this estimate, but it remains limited to 15 digits in base10 because

Solution 1:

for (n = 1; n < 256; n +=1 ) {
    console.log(n, Math.ceil(Math.log2(Math.pow(10,n))));
}

And the values match as far as I can tell

Solution 2:

log2(n)⌋ + 1

var n = 123;
console.log(n, (n >>> 0).toString(2) );
var nb = Math.floor( Math.log2(n) ) + 1;
console.log( nb );

Post a Comment for "Is There A Formula For Calculating Or Estimating The Number Of Digits Of A Binary Integer?"