Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 07-07-2005, 03:17 PM   #1 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Question Bitwise Operators

Now that I have finally learned how binary works and how to translate it I have moved on to learning about bitwise operators. I know that these four operators are ~ & | ^

But I don't understand what each of them do. I was given an explaination of each (such as the ~ toggles each bit from 1 to 0 and from 0 to 1) but I don't understand it. Could someone please explain what each of these bitwise operators do in an "easy-to-learn" way. Thanks to all who help-

::-Chase-::
Chase is offline  
Old 07-07-2005, 05:48 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Ok this might take a bit.

But lets go operator by operator.

Not
The only unary operatory (only works on one bit string) is the ~.

This operator does just what you described, it switches all 0's to 1's and all 1's to 0's.

For example
Code:
Starting Number: 11011001

~11011001 = 00100110
And

And (&) works on two bit patterns. It analyzes each corresponding bit position within each pattern. If both bits are the same (both are 1's or both are 0's), it results in a 1 in the final bit pattern, if they are different, it results in a 0.

For example
Code:
 
x     = 10010101
y     = 01011011
x & y = 00110001
Exclusive Or

Exclusive Or (^), sometimes referred to a XOR, also works on two bit patterns. It is the exact opposite of the and operator. If both bits are the same (both are 1's or both are 0's), it results in a 0 in the final bit pattern, if they are different, it results in a 0.

For example
Code:
These are the same numbers from the AND example.

x     = 10010101
y     = 01011011
x ^ y = 11001110

Notice how X^Y is the exact opposite of X&Y.
Or

The last bitwise operator is OR. This works very similar to the Exclusive Or. If both bits in the initial bit patterns are 1's, it results in a 0, for all other conditions it results in a 1.

For example
Code:
x     = 10010101
y     = 01011011
x | y = 11101110

I'll post back with some interesting uses of these later tonight. Right now it's time to get outta work!
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 07-07-2005, 06:47 PM   #3 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default

I don't know how to thank you! I have been trying to understand these operators for a VERY long time. After I read that tutorial I understand completely! The only thing I don't understand now is why you would want to use these, but I will wait for you to find time to post later, thanks again,

::-Chase-::
Chase is offline  
Old 07-07-2005, 08:16 PM   #4 (permalink)
 
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  
Old 07-09-2005, 11:12 PM   #5 (permalink)
 
Junior Techie

Join Date: Jun 2005

Posts: 94

ever_thus

Default

I'm afraid Bla! has made quite a few mistakes.

Or

If either of the two bits are 1 the result of this operation is a 1. Only if both bits are 0 is the result a 0.

What Bla! described was a nand operation. (This is an advanced operation which no language, to my knowledge, supports, so you won't need to learn it.)

and

if both of the two bits are 1 the result is a 1. In all other cases (that is, there is at least one 0) the result is 0.

What Bla! is an xnor operation. (Again, an advanced operation which you needn't learn.)


Regarding shifts I'm less sure of myself, but I'm pretty certain that the following is true.

In a left shift the spaces opened up on the right are filled with 0s. This is true of both arithmetic and logical shifts, though the Pentiums provide both operation to make it symettrical with right shifts.

In a logical right shift the spaces opened up on the left are filled with 0s. In a arithmetic right shift the spaces are filled with sign bit. That means that if the shift was applied to a positive number the spaces are filled with 0s; if it was applied to a negative number they are filled with 1s.
ever_thus is offline  
Old 07-10-2005, 12:35 PM   #6 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Wow, I apologize! I guess I'm a bit rusty on this stuff.

Thanks for the correction ever_thus!
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 07-10-2005, 02:50 PM   #7 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default

Thanks for the help, both of you. No sweat bla!, we all make mistakes. Well, now it's time to continue my study of C++. Thanks guys-
::-Chase-::
Chase is offline  
Old 07-14-2005, 08:24 AM   #8 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Try this code on for size....

neat property about the 'identity' feature of XOR'ing stuff...

Swap X & Y:
Method 1-
x = 1;
y = 2;
temp = x;
x = y;
y = temp;

Method 2-
x = 1;
y = 2;
x^=y^=x^=y;

Write it out on paper and see that it works A swap with no (explicitly created) temporary variable.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 07-14-2005, 09:56 PM   #9 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default

That's pretty awesome! Thanks for the neat bit of code!-
::-Chase-::
Chase is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On