By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

It's Friday and my mind has turned to mush!

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

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


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 pre-define 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
Jul 23 '05 #2

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 pre-define 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
}
Jul 23 '05 #3

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))
Jul 23 '05 #4

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

P: n/a
On Sat, 22 Jan 2005 02:15:24 +0100, Dietmar Meier
<us***************@innoline-systemtechnik.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 e-mail.
Jul 23 '05 #6

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
Jul 23 '05 #7

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/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

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

Jul 23 '05 #9

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

Jul 23 '05 #10

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/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11

P: n/a
Even better! Thanks again!

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.