Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of bits to represent a number

I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries.

Method 1:

function bitSize(num)
{
return Math.floor( Math.log(num) / Math.log(2) ) + 1;
}

Pass: bitSize( Math.pow(2, 16) -1 ) = 16
Pass: bitSize( Math.pow(2, 16) ) = 17
Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 
Pass: bitSize( Math.pow(2, 48) ) = 49

Method 2:

function bitSize(num)
{
var count = 0;
while(num > 0)
{
    num = num >> 1;
    count++;
}
return count;
}

Pass: bitSize( Math.pow(2, 16) -1 ) = 16
Pass: bitSize( Math.pow(2, 16) ) = 17
Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 1
Fail (Should be 49): bitSize( Math.pow(2, 48) ) = 1

Both methods fail to precision issues I think.

Can anyone suggest an alternative method that will work for numbers between 0 -> 2^53-1

Thanks.

like image 551
Jason Gooner Avatar asked Jun 20 '10 23:06

Jason Gooner


People also ask

How do you find the number of bits needed to represent a number?

For example, the number 15 can be represented in 4 bits, but 16 requires 5 bits. You can work this out by starting with the number 1, and doubling it until it gives you a value more than the number you need to represent, so this program can be based on the earlier challenge to work out the number of dots on each card.

How many bits are required to represent a?

Answer: In general, N bits (binary digits) are required to represent unique integer values. Eight bits are required to represent 256 unique values. Those values range in binary from 00000000 through 11111111, or 0 through 255 in decimal.

How many bits do you need to represent 7?

7 in binary is 111. Unlike the decimal number system where we use the digits 0 to 9 to represent a number, in a binary system, we use only 2 digits that are 0 and 1 (bits). We have used 3 bits to represent 7 in binary. In this article, let us learn how to convert the decimal number 7 to binary.

How many numbers can 1 bits represent?

The bottom line is that a single bit has two states: 0 and 1. Any value domain with more that two values in the domain cannot be represented using a single bit.


2 Answers

You can do:

function bitSize(num) {
    return num.toString(2).length;
}

The toString() method of Number takes the radix as an optional argument.

Here are some tests. Works on Chrome, Safari, Opera, and Firefox. No access to IE, sorry.

like image 128
Anurag Avatar answered Oct 09 '22 06:10

Anurag


Bitwise operations will only work reliably in Javascript for "integers" up to 32-bits. To quote from The Complete JavaScript Number Reference:

Bitwise operations are a bit of a hack in Javascript. Since all numbers in Javascript are floating point, and bitwise operators only work on integers, Javascript does a little behind the scenes magic to make it appear bitwise operations are being applied to a 32bit signed integer.

Specifically, Javascript takes the number you are working on and takes the integer portion of the number. It then converts the integer to the most number of bits that number represents, up to 31 bits (1 bit for the sign). So 0 would create a two bit number (1 for the sign, and 1 bit for 0), likewise 1 would create two bits. 2 would create a 3 bit number, 4 would create a 4 bit number, etc…

It's important to realize that you're not guaranteed a 32bit number, for instance running not on zero should, in theory, convert 0 to 4,294,967,295, instead it will return -1 for two reasons, the first being that all numbers are signed in Javascript so "not" always reverses the sign, and second Javascript couldn't make more than one bit from the number zero and not zero becomes one. Therefore ~0=-1.

So bitwise signs in Javascript are up to 32 bits.

As Anurag notes, you should simply use the built-in num.toString(2) in this situation instead, which outputs a minimal length string of ASCII '1's and '0's, which you can simply take the length of.

like image 20
fmark Avatar answered Oct 09 '22 06:10

fmark