Why Bitwise Operators?

Jul 20, 2005

Why would one use bitwise operators? I can program in various languages in
some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise
operators before, but never understood why anyone would use them - any real
world examples or ideas? Examples follow (that I am reading in my Core
JavaScript Guide 1.5).

15 & 9 yields 9 (1111 & 1001 = 1001)
15 | 9 yields 15 (1111 | 1001 = 1111)
15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
in addition there are AND,OR,XOR,NOT and left and right shifts which would
only extend this post longer than nescessary...

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

View 11 Replies


ADVERTISEMENT

Using Bitwise Operators To Post Different Alert Messages

Aug 25, 2004

I have been looking for a way to give something back to this forum as it has helped me greatly, and I am hoping that this might be of value to someone.

Basically I was just tired of consistantly commenting and uncommenting out alerts in my code so I wrote some code that would allow me to keep my alerts and only show them when I wanted to, and only the types of alerts I wanted to show.

I thought that using bitwise operators would give me the flexibility to specify different options and a quick way of finding out what options were wanted. There are other ways of doing this of course, and this is one approach.

The bitwise OR operator "|" says if one of the two vars has the bit set, then set the bit.
the bitwise AND operator "&" says if both of the two vars has the bit set, then set the bit.

It starts with my bitwise values:

var NONE = new Number(0);// 00000000
var ALL = new Number(1);// 00000001
var INFO = new Number(2);// 00000010
var LOCAL = new Number(4);// 00000100
var PARAM = new Number(8);// 00001000
var COUNT = new Number(16);// 00010000

These values are really important. Earlier I used 0 through 5 and because of the way bitwise operations work, COUNT was equivelant to ALL and PARAM. what you are doing is blending bits and then getting a value. So, in the 0 through 5 approach, COUNT was 00000101 (5). When I OR'd ALL with PARAM I got 00000101 which was a result of 00000001 with 00000100 (4)! So be really careful if you go this route to double check that no combination of vars will equal other vars.

In our constructor we get the value thus:

function FormCheckBase( objForm, blnStateIsDebug, sCulture )
{
this.Form= eval('document.' + objForm);
this.Debug= new Number( blnStateIsDebug );
this.Culture= new String( sCulture );
}

with this call:

objFrm = new FormCheck( 'FormName', INFO | LOCAL | PARAM, sCulture );

Then in the instance methods of the FormCheck class, I wrap my alert boxes.

if ( this.ShowErrorAlert( this.PARAM ) ) // or whatever other value you want.
{
alert( sWhoAmI + "iArrayLength: " + iArrayLength );
alert( sWhoAmI + "blnBlank: " + blnBlank );
}


ShowErrorAlert does a bitwise "AND" to see if we have a match. We are also testing for ALL, and if that is passed originally, we are returning a true to show the alert.

FormCheckBase.prototype.ShowErrorAlert = function( iAlertBit )
{
var bitCompareResult = new Number(0);
var showMe = new Boolean(false);

bitCompareResult = (this.Debug & this.ALL);
showMe = ((bitCompareResult == this.ALL) ? true : false );
alert( showMe );
if ( showMe == true )
return showMe;

bitCompareResult = (this.Debug & iAlertBit);
showMe = ((bitCompareResult == iAlertBit) ? true : false );

return showMe;
};

I also did a couple of helper functions that would change the Debug var before I went into a method and reset it after I was done. Sometimes you might not want every single method to start showing alert boxes. So I did a SetDebugBits that sets the value of Debug and GetDebugBits that gets the current value of Debug. Call GetDebugBits first to store the current value, then call SetDebugBits to set it to whatever you want to use, and then make another call to SetDebugBits with the original value to set it back.

We mostly call our FormCheck constructor with the value of NONE, and then work on a method by method basis as we have lots of methods in the class.

View 7 Replies View Related

Question About Operators

Apr 26, 2006

Searched W3Schools site for information on JavaScript operators.

It doesn't seems to be an all inclusive source.

It doesn't have ? or :

I did learn what ? means from a book... But what does : mean/do?

View 5 Replies View Related

Which Operators Can Be Used With An IF Statement

May 3, 2010

a. only logical operators

b. only comparison operators

c. both logical and comparison

d. you cannot use operators with an if statement.

this is from my javascript book review for the chapter.

you can use both right? sounds silly i know but just want to make sure for my test.

View 4 Replies View Related

JQuery :: Logical Operators In Selectors?

Jul 1, 2010

Ok, this seems easy, but I am struggling. I have this...

$("a[href$='mp4']").click(function () {
But I want it to work for mp4 or mov or more. Something like...
$("a[href$='mp4'||href$='mov']").click(function () { <-- which doesn't work apparently

View 2 Replies View Related

Creating Calculator - How To Execute Operators

Jul 18, 2010

We are required to create a javascript calculator. The page should has 3 text box, first and second text box would allow the user to input numbers, the third textbox will show the answer. the arithmetic operations should be in radio button, drop-down list and the select option. I'm still working on the radio button and i don't how to execute the operators. How to grab the inputted number from the textbox and how to execute the operators.

View 1 Replies View Related

Conditional Operators Versus If Else Statements

Apr 28, 2011

I'd like to ask if it is possible to use conditional operators
var = condition ? var1 : var 2
To do the same job as the if-else statements I wrote in red below:
<p id="text">change text colour</p>
<br />
<a onclick="allsorting();">sort in both ascending & descending orders</a>
<div></div>
<script>
/* if & else */
function changetext(){
document.getElementById('text').onclick = function (){
swapcolour(this);
}}
function swapcolour(text){
if(text.style.color == 'black'){
text.style.color = 'red';
} else {text.style.color = 'black'}
} window.onload = changetext;
</script>

View 25 Replies View Related

Creating URLs From Function Call Operators

Jul 20, 2005

Is it possible to create URLs from function call operators?

For example, I'm trying to program an onClick function which will load a
webpage in one frame and an image in another, based on the name of the
button which is being clicked. So clicking the button with name="help" will
open "webpages/help_page1.html" in one frame, and "images/help.jpg" in
another.

To do this I was hoping that the following would work, where the function
had been called by

function activate(buttonName) {
webpageURL="webpages/"+buttonName+"_page1.html"
imageURL="images/"+buttonName+".jpg"
parent.frame1.location.href=webpageURL
parent.frame2.picture.src=imageURL
}

But this doesn't work, because the browser (IE6) looks for
"webpages[object]_page1.html" and "images[object].jpg".

View 2 Replies View Related

Slide Toggle Script And Conditional Operators

Aug 20, 2011

I've got a slide toggle script online and have got just one bit that I cannot figure out why. What is (toggled = !toggled)? What does it mean? Does that mean toggled = false? But I tested it, it seems not like that. And once the slide is toggled, it should use the minheight. Then the condition for var Height must be the opposite of (toggled = !toggled). Then what is (toggled = !toggled) like? Is that like (toggled != !toggled)?

<!DOCTYPE html>
<html>
<head>
<title>Avinash</title>

<style>
#slider {
margin:0px auto;
padding:0px;
width:400px;
border:1px solid #000;
background:#0f0;
height:0px;
overflow:hidden;
} .....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved