![]() |
Bitwise Operators...
I have read/looked through like 2 books and 1 online guide about "bitwise operators" such as >> and <<... I just can't express the amount of confusion I have towards these operators xD.
Example: Quote:
So if someone could explain them more in-depth, more importantly how they are used and what the outcome becomes :P, that would be great ^_^. -- Them being the commands like >>, &, << and |. |
Re: Bitwise Operators...
Bitwise operators work on the specific bits of the value you're modifying. For this reason they'll only work on integer types (floating point numbers are fancy magic). Here's a rough explanation of some of them work (using 8 bit values):
Bitwise shift (<<, >>) just shifts the bits one way or another. Examples: left shift: 5<<2: :
5 in binary is 00000101signed: -16 >> 2 (aka, arithmetic shift which preserves signed-ness bit) :
-16 in binary is 11110000:
132 in binary is 10000100signed: -16 >>> 2 (note: not C syntax, this wouldn't happen in C for signed values) :
-16 in binary is 11110000:
132 in binary is 10000100The AND operator is pretty simple. It compares each corresponding bit pair and sets the result bit if both bits in the pair are set. This is really useful for setting flags. Examples: 255 & 170 == 170 :
11111111:
11110000:
00111111The OR operator sets the bit if one or the other is set: 32 | 64 == 96 :
01000000:
10000001value | 1<<n will set the nth bit from the right).The XOR operator sets the bit only if exactly one of the compared values has it set: 64 ^ 32 == 96 :
01000000:
10000001 |
Re: Bitwise Operators...
Thank you so much for having such a long post O_O. Really helped me understand them ^_^.
But I have a few more questions :P.
|
Re: Bitwise Operators...
Jimbo did an excellent job explaining, so I won't re-hash that, but regarding the >> operator, I believe it determines whether or not to preserve the sign bit based on the data types involved. For example:
:
|
Re: Bitwise Operators...
It's generally not used to do math, because the fact is the difference in speed is pretty much negligible.
Take, for instance, these two programs: :
:
The behaviour of each program is identical, except the first shifts and the second multiplies. They both do a billion shifts or a billion multiplications in the same amount of time. This is because they are both constant time operations, and most modern compilers will do optimizations under the hood that make these differences negligible. The place where bit manipulation becomes most useful is in algorithm design, where it becomes valuable to convert a state or vertex into a distinct value for hashing and traversing. For example, say we are keeping track of a row of 8 coins, that are either heads or tails. :
Coin: 1 2 3 4 5 6 7 8If 1 is heads, and 0 is tails, then this can be easily represented by a number: 00101011That is the number 43 (32+8+2+1). So you might be wondering how we actually get the number 43 from an array of heads and tails. It can be done by reading each face, while setting and shifting the appropriate bits with bitwise operators. :
Bitwise operators also give us the following operations on our set of coins:
Now, on the spot, I'm going to try make up a programming problem that would require all of this together... hmmm... okay. Here goes: :
Rob is playing Amy in a game of "Coins". "Coins" is a popular game played with 8 pennies, lined up in a row, initially in the configuration: If you know how to write a recursive function, and want to try this out, it should be a great excercise in the advantage of bitwise operators. Here's my solution. Note that it uses every available bitwise operator. Also note that without converting the sequence of coins into an integer, we could not use a cache where the coin setup is an index into the array. Convenient! :
#include <stdio.h>So, in conclusion, bitwise operators aren't generally used to do math. They should be used just as they were intended: to manipulate and access individual bits, in order to accomplish something more easily and efficiently. Trying to solve this problem without bitwise operators would be extremely painful (and inefficient)! |
Re: Bitwise Operators...
Quote:
|
Re: Bitwise Operators...
It's relatively constant. For 4 byte integers, it is regarded as constant time, as a result of the CPU's cache and the relative number of cycles required.
From my algorithm's textbook: Quote:
|
Re: Bitwise Operators...
Quote:
The relative performance of operations (eg multiply by two vs a bit shift) actually depends on the machine your program will run on (eg the instruction set). The majority of good quality, modern, compilers will pick the operation that works best, depending on optimisation settings (eg compiling for speed, compiling for minimum executable file size, etc). Unless you're writing your own optimising compiler, you are writing highly critical code in assembler, or you have encountered a compiler bug there is usually no need to worry about such things. |
Re: Bitwise Operators...
My job often requires that I take the data created by someone else's program - and extract the parts I need into something we can use.
When extracting useful data from various binary files - I often have to use the AND operator ... A common use of bit-wise math is encoding several flags into a single byte. Suppose the program needed to store these 8 YES/NO flags: IsPatient_YN IsDoctor_YN IsHealthPlan_YN HasID_YN HasPhone_YN HasFax_YN IsActive_YN IsDeleted_YN This could be stored as a series of single-byte entries - each entry using the single character "Y" or "N". However, using bit-wise operators - the program could store all 8 of these flags in a single byte... :
:
(This is a small savings - and in these days with large storage devices - some would argue that the space saved is not worth the extra work. But if you're writing a program that deals with millions of individual records - this can be a significant savings...) A similar algorithm is (was) used in the original DOS directory entries ... the 'archive-bit' was set on a directory entry in certain circumstances. The other bits in that byte controlled the 'read-only' flag - the 'system-file' flag, etc. |
Re: Bitwise Operators...
Hey, I have been reading through all of the replies and thanks much for all the time you have put into this ^_^. I'm still pretty confused, but that is just because I haven't fully read through them yet :P.
2 Quick Questions for now since they are on my mind :P
|
| All times are GMT -5. The time now is 4:23 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC