PDA

View Full Version : bitwise shift operators.


x_goose_x
11-17-2002, 11:32 PM
Recently I've read up on bitwise shift operators. I now understand how they work, but don't really see a use for them. Was just wondering if anyone could enlighten me on how they could put to a practical use.


9<<2

ASCII 9 = 1001 in binary shifted 2 bits to the left becomes 100100 which equals 36.


I also dont see a use for bitwise operators:

15 & 9
1111 & 1001 = 1111

It's not that I dont know how to do them, it's quite simple. I just can't seem to find a case where I'd need this. A snippit of code or example would be great. Thnx.

JustAsking
11-18-2002, 12:10 AM
Below is some code I found a while ago, pretty simple probably not much use, but may help you understand how to use it. I found using bitwise operators allowed me to generate code that is more efficient than using other operators (e.g. Math).

Here's the code:

<html>
<head>

<title>What's for breakfast?</title>

<script language="JavaScript"><!--

if (window.focus) self.focus();

//create array of possible responses
var response = new Array()
response[0] = "Coffee, juice, and beer, to cover all bases."
response[1] = "Triple espresso, to bring on the necessary meltdown."
response[2] = "Large strong coffee, for times of confusion and/or balance."
response[3] = "Kava kava juice, to take the edge off."
response[4] = "Nothing."
response[5] = "Large strong coffee, for times of confusion and/or balance."
response[6] = "Beet, carrot, and spinach juice, to fend off impending doom."
response[7] = "Coffee, juice, and beer, to cover all bases."

function getResponseBitwise(form) {
var numChoices = 3;
var combo = 0;
for (i=0; i<numChoices; i++) {
combo |= form.elements[i].checked << numChoices-i-1;
}
form.responseText.value = response[combo];
}

//--></script>

</head>

<body bgcolor="#ffffff">

<p>
<table width=470 border=0 cellpadding=0 cellspacing=0>
<tr><td><img src="/Images/black.gif" width=313 height=3 hspace=0 vspace=0></td>
<td><img src="/Images/black.gif" width=157 height=3 hspace=0 vspace=0></td></tr>
<tr valign=middle>
<td width=313 bgcolor=#ffffcc align=left nowrap><font face="Arial,Helvetica" size=+1><b> SuperScripter</b></font></td>
<td width=157><img border=0 src="/Images/logosubc.gif" width=157 height=38 alt="BUILDER.COM" hspace=0 vspace=0></td>
</tr>
<tr><td colspan=2><img src="/Images/black.gif" width=470 height=3 hspace=0 vspace=0></td></tr>
</table>
<p>

<form name="drink">

<table border=0 cellpadding=5 cellspacing=3>
<tr><td colspan=2><font face="Arial, Helvetica" size=-1>
<b>Check all that apply:</b>
</font></td></tr>

<tr bgcolor="#eeeeee">
<td><font face="Arial, Helvetica" size=-1><input type="checkbox" name="peace"></td>
<td><font face="Arial, Helvetica" size=-1>You're completely at peace with the world and your place in it.</td>
</tr>

<tr bgcolor="#eeeeee">
<td><font face="Arial, Helvetica" size=-1><input type="checkbox" name="fog"></td>
<td><font face="Arial, Helvetica" size=-1>Everything appears to be fine, but you're aware of a nagging sensation in the back of your head.</td>
</tr>

<tr bgcolor="#eeeeee">
<td><font face="Arial, Helvetica" size=-1><input type="checkbox" name="wig"></td>
<td><font face="Arial, Helvetica" size=-1>You're feeling overwhelmed and paralyzed by the infinitude of life's choices.</td>
</tr>

<tr><td colspan=2>
<input type="button" value="Submit" onClick="getResponseBitwise(this.form)">
<input type="reset" value="Clear">
<p>
<font face="Arial, Helvetica" size=-1><b>Your ideal breakfast beverage for today:</b></font><br>
<textarea name="responseText" wrap="virtual" rows="3" cols="50"></textarea><br><br>
</td></tr>
</table>
</form>

</body></html>



See this bitwise operator code:

var combo = form.elements[0].checked << 2
| form.elements[1].checked << 1
| form.elements[2].checked << 0


It could also be done like this, but is not as efficient:

var combo = form.elements[0].checked*(Math.pow(2,2))
+ form.elements[1].checked*(Math.pow(2,1))
+ form.elements[2].checked*(Math.pow(2,0));

jkd
11-18-2002, 12:47 AM
With higher level languages, they are almost pointless. However, even in C++ you'll find a need for them, but in general you don't. Bitwise operations are faster than arithmetic though, albeit more complicated.

The only thing I still use them for is flagging in multiple booleans into a single variable:

var flags = 0;
flags |= Math.pow(2, 0);
// sets first bit to 1
flags |= Math.pow(2, 3);
// sets the fourth bit to 1

// flags looks like: "1001"

And you'll find that used in several programming languages that don't support unlimited arguments such as in Javascript.

glenngv
11-18-2002, 01:13 AM
one practical use of bitwise operators is in detecting SHIFT, ALT, CTRL keys for NS6.

read this thread:
http://codingforums.com/showthread.php?s=&threadid=9304

x_goose_x
11-18-2002, 01:22 AM
Thnx for the responses. I kinda see a use for it now.