473,399 Members | 2,858 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,399 software developers and data experts.

Bittest in Javascript?

I need a Javascript function that can do a Bit Test on a numeric
value. IOW, given a numeric value (a 16 bit integer value) and a bit
number, tell me whether that bit is 1 or 0.

I have no idea how to do this.

Help?
Jan 1 '06 #1
3 3027
Martin <ma**********@comcast.net> writes:
I need a Javascript function that can do a Bit Test on a numeric
value. IOW, given a numeric value (a 16 bit integer value) and a bit
number, tell me whether that bit is 1 or 0.


Since Javascript doesn't have a 16 bit integer type, you will just
have your value in the range 0..65535 in a number value. Bit
operations in Javascript will convert their operand to a 32 bit
integer before operating, so there is plenty of room for your numbers.

To test that the n'th bit is set, you use a mask with only the n'th
bit set. One such can be created using the left shift operator:

var mask = 1 << n;

Then you do a binary "and" on the number and the mask, to make sure
all other bits are nulled. If the result is non-null (and equal to the
mask) then the n'th bit was set in the original number.

var bitIsSet = (number & mask) != 0;

In total, a function to test a single bit:

function bitIsSet(number, bitnum) {
return (number & (1 << bitnum)) != 0;
}

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 1 '06 #2
Martin wrote:
I need a Javascript function that can do a Bit Test on a numeric
value. IOW, given a numeric value (a 16 bit integer value) and a
bit number, tell me whether that bit is 1 or 0.

I have no idea how to do this.


Use the Bitwise AND operator `&', and the Bitwise Shift Left operator `<<':

if (n & (1 << b))
{
/*
* b-th bit of n if n is converted to a 32-bit integer,
* the least significant bit of n being the zeroth one
*/
}

Is this for class? Because it really is programming basics -- RTFM.

<URL:http://jibbering.com/faq/#FAQ3>
PointedEars
Jan 1 '06 #3
JRS: In article <ig********************************@4ax.com>, dated
Sun, 1 Jan 2006 13:44:53 local, seen in news:comp.lang.javascript,
Martin <ma**********@comcast.net> posted :
I need a Javascript function that can do a Bit Test on a numeric
value. IOW, given a numeric value (a 16 bit integer value) and a bit
number, tell me whether that bit is 1 or 0.


For reasonable values, N >> X & 1 returns the value of bit X of the
integer N.

--
© 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.
Jan 2 '06 #4

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

Similar topics

0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.