473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
11 1076
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
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

<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
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
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
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
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
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
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
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
Even better! Thanks again!

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: michaaal | last post by:
How can I get a list of the date of every friday of the year? I realize this code is wrong, but maybe you'll see what I am after... Sub GetFridayList() For x = 1 to len(Date(2004)) if...
3
by: Peter | last post by:
Has anyone written any code to find the date of every other Friday of the year? Is there an easy way of doing this?
11
by: CK | last post by:
Hi All, Looking for a simple function to return the date for the follwing Friday. I send out emails from time to time and I would like a function that dynamically returns the date for the...
5
by: vj | last post by:
I'm doing: a = now() delta = ReltaiveDateTime(days=+6, weekday(mx.DateTime.Friday, 0)) Next Friday: a+delta a: march 23 a+delta: Gives me March 31st and not March 24th Any ideas?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.