faqts : Computers : Programming : Languages : JavaScript : Language Core : Numbers

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 11 people (64%) answered Yes
Recently 3 of 7 people (43%) answered Yes

Entry

How can I find the bit length of an integer?
How can I find the number of bits an integer has?

Mar 25th, 2000 16:26
Martin Honnen,


The following function calls toString(2) to convert to binary notation 
and then takes the length:

function bitLength (n) {
  return n.toString(2).length;
}

Examples:
  alert(bitLength(2))
  alert(bitLength(16))