P: n/a

Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit
is represented? In other words, 2 = bit 2, 128 = bit 8.
Just not sure how to do it simply in JS without resorting to a loop.
Steve  
Share this Question
P: n/a
 sn****@mxlogic.com wrote: Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit is represented? In other words, 2 = bit 2, 128 = bit 8.
If you want to calculate it, use:
bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;
I would rather predefine an array ...
var i, bits = []; for (i=0; i<8; i++) bits[1 << i] = i + 1;
.... and use that array when needed:
bit = bits[flag];
ciao, dhgm  
P: n/a

Dietmar Meier wrote: sn****@mxlogic.com wrote:
Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit is represented? In other words, 2 = bit 2, 128 = bit 8.
If you want to calculate it, use: bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;
I would rather predefine an array ... var i, bits = []; for (i=0; i<8; i++) bits[1 << i] = i + 1; ... and use that array when needed: bit = bits[flag];
ciao, dhgm
Alternatively, you could do something like this:
function which_bit(mask) {
return (mask & 1)? 0 // bit 0 is set
:(mask & 2)? 1 // bit 1 is set
:(mask & 4)? 2 // bit 2 is set
:(mask & 8)? 3 // bit 3 is set
:(mask & 16)? 4 // bit 4 is set
:(mask & 32)? 5 // bit 5 is set
:(mask & 64)? 6 // bit 6 is set
:(mask & 128)? 7 // bit 7 is set
: 1; // no bits are set
}  
P: n/a

<sn****@mxlogic.com> wrote in message
news:11********************@c13g2000cwb.googlegrou ps.com... Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit is represented? In other words, 2 = bit 2, 128 = bit 8.
Just not sure how to do it simply in JS without resorting to a loop. Steve
or use recursion:
var x = 1;
function theBit(theFlag){
if (Math.pow(2, x) == theFlag){
return x +1;
}else {
x++;
return theBit(theFlag);
}
}
var num = 8;
document.write("The number " + num + " is bit " + theBit(num))  
P: n/a

PS  I like Dietmar's solution best 
and
my recursion will crash unless the number is exactly a power of 2.  
P: n/a

On Sat, 22 Jan 2005 02:15:24 +0100, Dietmar Meier
<us***************@innolinesystemtechnik.de> wrote:
[snip] If you want to calculate it, use: bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;
It would be more efficient to use the Math.LN2 constant rather than
calculating log(2) repeatedly. There's also the question of accuracy.
[snip]
Mike

Michael Winter
Replace ".invalid" with ".uk" to reply by email.  
P: n/a

Michael Winter wrote: bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;
It would be more efficient to use the Math.LN2 constant rather than calculating log(2) repeatedly.
Definitely.
There's also the question of accuracy.
It's accurate up to a flag value of 2^27 in common browsers, what
should be enough in most cases. But I'd prefer the array version
anyway.
ciao, dhgm  
P: n/a

JRS: In article <11********************@c13g2000cwb.googlegroups.c om>,
dated Fri, 21 Jan 2005 16:25:43, seen in news:comp.lang.javascript, sn****@mxlogic.com posted : Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit is represented? In other words, 2 = bit 2, 128 = bit 8.
Just not sure how to do it simply in JS without resorting to a loop.
That makes sense only if you are sure that exactly one bit is set.
function BF(N) {
return 1 + !!(N & 0xAA) + 2*!!(N & 0xCC) + 4*!!(N & 0xF0) }
x = [BF(0),BF(1),BF(2),BF(4),BF(8),BF(16),BF(32),BF(64) ,BF(128)]
var Ob = {x1:1,x2:2,x4:3,x8:4,x16:5,x32:6,x64:7,x128:8}
function BG(N) { return Ob['x'+N] }
y = [BG(0),BG(1),BG(2),BG(4),BG(8),BG(16),BG(32),BG(64) ,BG(128)]
function BH(N) { var K=0
while (N) { K++ ; N>>=1 }
return K }
z = [BH(0),BH(1),BH(2),BH(4),BH(8),BH(16),BH(32),BH(64) ,BH(128)]
A = [x,' ',y, ' ',z]
BH() seems best; it should be fast enough, and extends to 32 bits.

© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

Thanks everyone! Wow... didn't expect such a great response... to keep
things simple, I'll go with the array lookup method. Hopefully, others
will find this thread useful too.
Steve  
P: n/a

Incidentally, after I posted my question I came up with the following
(using a loop though!!)
var i = 0;
while (Math.pow(2, ++i) != bitFlagValue) { }
// set bit is in 'i'
Obviously, need to make sure an endless loop doesn't ensue.
Steve  
P: n/a

JRS: In article <11*********************@f14g2000cwb.googlegroups. com>,
dated Mon, 24 Jan 2005 14:41:48, seen in news:comp.lang.javascript, sn****@mxlogic.com posted : Incidentally, after I posted my question I came up with the following (using a loop though!!)
var i = 0; while (Math.pow(2, ++i) != bitFlagValue) { } // set bit is in 'i' Obviously, need to make sure an endless loop doesn't ensue.
Never test for equality or inequality without considering whether
testing for > >= < <= would be safer.
If B has one of the lowest 8 bits set, this returns the index, 0..7 :
k = 256 ; j = 7 ; while ((k/=2) > B) j
If B has more than one set, it returns the index of the highest. If B
has any other bits set, it returns 7, unless B is negative.
This may be best, if B is certainly positive :
j = 0 ; while (B = B>>1) j++

© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

Even better! Thanks again!   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 981
 replies: 11
 date asked: Jul 23 '05
