View Single Post
Old 07-07-2005, 08:16 PM   #4 (permalink)
bla!!
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Ok, happy hour's over

The main use I've found for these is in optimizing your own code to run faster on specific machine's.

To explicitly use them, a knowledge of Assembly is needed. They are used quite a bit to set conditional flags.

Here's a couple of neat aspects of bitwise opearators.

They can be used to zero out variables.

Code:
x      = 01101001
~x     = 10010110
x & ~x = 00000000

x & ~x will always be 0.
They can be used as an identity function

Code:
x = 01101001
y = 00111010
z = x ^ y
z = 01010011

z ^ x = 001110010 = y

So (x ^ y) ^ x = y
and (x ^ y) ^ y = x
You can implement all of the arithmetic functions using bitwise operators also, and many of them will run faster than an un-optomized version.

Also, you're probably going to learn about <<, >> and >>>. << Is the left shift operator, >> Is the logical right shift, >>> is the arithmetic right shift.

Left Shift
The arithmetic left shift takes a bit pattern and shifts it to the left the number specified after the << sign, filling the new spaces with the farthest right bit of the original number.

For example
Code:
x      = 01001011
x << 3 = 01011000

y      = 01001010
y << 3 = 01010000
Logical Right Shift

>> Is the logical right shift. This shifts a bit pattern to the right by the number following the >> sign, filling the extra bits with 0's.

For example
Code:
x      = 01001011
x >> 3 = 00001001

y      = 10110101
y >> 3 = 00010110
Arithmetic Right Shift

>>> Is the arithmetic right shift. This does the same as the logical right shift, but instead fills the extra bits with copies of the farthest left bit.

For example
Code:
x       = 01001011
x >>> 3 = 00010110

y       = 10110101
y >>> 3 = 11110110

__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline