   c++ bitwise
  Hi, i'm being confused by bitwise. i understand how it works, but why. can someone tell me what the point of bitwise is and when you could use it?thanks   
  obvious answer: changing bits.common use for video games: going from one color format to another. (ex: going from 16bit color to 24 bit, or vice versa)also can be used for CPU without floating-point capabilities such as: fixed point calculations;  well for network traffic it's useful for data packing, since you want to make sure your data is taking up the smallest space possible [no point in using a whole int if your numbers range from 1 to 62].  Also, if you're clever in your design, you can use bit tests to perform a couple checks in a single pass against a set of flags or properly packed data.  On that note, its also useful for flags, or instances when you want to be able to pass a rather detailed description using very little memory [32 'yes/no' questions in a single 4 byte int].They have uses, and those who love to use them [like i do] will find all kinds of things, but if they don't fit into your programming style, then there isn't much a point in trying to wedge them in where they don't belong.   ;  These days it's often used to send options to a function where it would be entirely inconvenient to have a seperate argument for each option.  That is, something like this:int foo(bool option1, bool option2, bool option3, ..., bool option32);and is called like this:foo(true, true, false, ..., true);This can be shorted to:int foo(unsigned int options);and is called like this:foo(OPTION_A | OPTION_S | OPTION_T);This looks a little confusing, so perhaps a slightly more real world example (as in, not abstract as above, but not necessarily in real production code) is in order:// Only one bit set in each 'flag', as this is called#define FULL_SCREEN   0x00000001#define SCROLL_BAR_X 0x00000002#define SCROLL_BAR_Y 0x00000004#define NO_RESIZE  0x00000008#define NO_MINIMIZE  0x00000010#define WHATEVER0x00000020// And so onWINDOW_CLASS Window;Window.Appearance(FULL_SCREEN | SCROLL_BAR_Y | NO_RESIZE | NO_MINIMIZE);//  And inside the Appearance method:Window::Appearance(unsigned int options){  if(options & FULL_SCREEN)  {    this->full_screen = true;  }  if(options & SCROLL_BAR_X)  {    this->scroll_bar_x = true;  }  if(options & SCROLL_BAR_Y)  {    this->scroll_bar_y = true;  }  // etc}Note I probably should have avoided defines, but I just wanted a quick example of how you Might use bitwise operators in your code.  Remember: just because something is there, doesn't mean you need it.;  before you can understand BitWise , you need to understand binary.computers use combination of bits(1 or 0) to represent everything (letters , commands , colors etc..). so the ability to set every single bit is quite usefull.some applications uses bits to set a lot of flags in bytes (8 bits).for example the flag might be %0000 0000 , by using the OR (|) operator with 128 (%1000 0000) then with 64 (%0100 0000) , the resultunt flage would be %1100 0000. so a function can check for every bit begin set(1) or clear(0) and act upon that ,instead of using a lot of booleans.bitwise operators are :Operator ANDThe AND operator (&) is a single ampersand. When you use AND on two bits, the result is1 if both bits are 1, but 0 if either bit is 0 (or if both are 0). Think of this like so: The resultis 1 if bit 1 is set and if bit 2 is set.Operator ORThe second bitwise operator is OR, a single vertical bar (|). When you OR two bits, the resultis 1 if either bit is set or if both are set.Operator Exclusive ORThe third bitwise operator is exclusive OR (^). When you exclusive OR two bits, the resultis 1 if the two bits are different.The Complement OperatorThe complement operator (~) clears every bit in a number that is set and sets every bit thatis clear. If the current value of the number is 1010 0011, the complement of that number is0101 1100.Shift OperatorsThe shift right (>>) and shift left (<<) operators are a little confusing because they are exactlythe same as the insertion and extraction operators. Trust me on this one; the compiler is ableto tell the difference by the context in which the operators are used.The shift right operator shifts all the bits one position to the right and fills the opening witha zero. So, if the number is 0110 0011, the shift right will make it 0011 0001. The bit on thefar right is discarded.The shift left operator shifts all the bits one position to the left. With the same number, 01100011, the shift left will make it 1100 0110.;  Some practical uses:IO operations are more expensive than bitwise manipulations so saving a player's option settings by packing them into a single int (or a few ints depending on the number of options). Any sort of unlockable system would surely make use of bitwise operations. Each unlockable item would be saved in a file as a single bit of an int. Rather than reading in 1000 integers to find out which of the 1000 unlockable items are unlocked you can simple pack them into just over 30 ints. Doing this will also save space on the storage device (especially when there are lots of data). This is especially important for things like memory sticks and other not so big or fast devices.   ;  Another use I didn't mention would be for instances where you don't have much memory available you can pack in related data into a single int. Some people might find the code ugly with bitwise operations being performed, but it's nice in the sense that you only really have one variable for all this related data.Naturally the data packed into an int doesn't have to be related, but it makes the code confusing in my mind if you store say a game option flag in a packed int used mainly for colour data.   ;  An example of using bitwise operations:Making a hashing algorithm that takes a string and returns a value (usually of size of some power or 2), with the input data being a string.Hash values work best when the bits are scrambled and the changeable parts of the data are spread evenly across all bits of the output value.Simple:  string in   1 byte hash value out....loop all chars in string and xor the char's 8 bits together...(unfortunately with printable strings, the majority ate a-z A-Z 0-9  so there wil be clustering in the upper bits)So you would then do some shifting of the chars to force the highly changeablebits to be also applied in the upper bit part of the output.(i would do this by xor-ing with every other byte shifted up  by 4  into a 2 byte variable and then xoring the low byte with the high byte at the end);  I always liked the XOR swap algorithim:void swap(int *element1,int *element2){*element1^=*element2;*element2^=*element1;*element1^=*element2;};  Oooh, just thought of another use.  Some games allow the user to enter a password as a means of saving a game without actually saving any data.  Like Metroid for the Nintendo.  You could have, say, one range of bits to store the location.  Bit one being on means you are at location X, while bit two being on means you're at location Y.  Then another range is for equipment.  Bit 40 means the user has... a cool gun.  Another few bits stores the ammo.  And so on.  Here for the thread that I was thinking of:http://www.gamedev.net/community/forums/topic.asp?topic_id=394981&whichpage=1&#2617709   