Chocoboko 124 Report post Posted March 4, 2003 Hello, I am unfamilar with bitwise operators such as >> and << (i.e. myvalue >> 8. I have seen them used in many codes and I figure I need to learn them if I plan to write values into and out of files greater than 8 bits. Can someone teach me briefly or point me to a tutorial online where I could look it up? Thank you. Any help would be greatly appreciated. 0 Share this post Link to post Share on other sites

Zipster 2365 Report post Posted March 5, 2003 Those are shifting operators. In general, whenever you shift a number in a given base, the value is either increased on decreased by a power of that base. For example, shift a base-10 number to the left 2 places, and it has the effect of multiplying the value by 100 (10^{2}). Shift a base-10 number to the right a single place, and it has the effect of dividing the number by 10 (10^{1}).It works the same way in base two with the bitwise operators. When you shift a number using a bitwise operator, you are multiplying or dividing by powers of two, much like you were multiplying or dividing my powers of ten in base-10. So something like myvalue >> 8 is shifting to the right eight places, which is like dividing by 2^{8}, or 256.[edited by - Zipster on March 5, 2003 3:40:59 AM] 0 Share this post Link to post Share on other sites

Oluseyi 2103 Report post Posted March 5, 2003 First off, I think it''s helpful to point out that the bitwise shift operators are overloaded for C++ streams as the insertion and extraction operators (I''m sure you know it, but it''s necessary to reiterate to avoid confusion).The bitwise operators are <<, >>, ~, & and |.<< (Left bitwise shift): This shifts every bit in a value n places over to the left, where n is the right-hand side parameter.unsigned short int x = 255;// x = 0000000011111111 in binary notationx << 5;// x is now 0001111111100000 in binary, 8160 in decimal Left bitwise shifting by one place is equivalent to multiplying by 2, while shifting by n places is equivalent to multiplying by 2^{n} (verify: 255 x 2^{5} = 255 x 32 = 8160). Thus bitwise shifting can be used to effect rapid multiplication by a constant value which is either a power of 2 or can be decomposed into powers of 2.>> (Right bitwise shift): This is the bitwise inverse of the left bitwise shift, moving bits over to the right and effectively dividing by powers of 2. The result is the floor of any fractional value.unsigned short int x = 255;// x = 0000000011111111 in binary notationx >> 5;// x is now 0000000000000111 in binary, 7 in decimal (verify: 255 / 32 = 7.96875 = 7(floor))~ (Bitwise inversion): This operator inverts every bit in a value, from 0 to 1 and from 1 to 0. Note that this is a unary operator (it takes only one parameter, to the right, and though operator =~ might deceive one into thinking it is binary, it''s really a combination of inversion and assignment).unsigned short int x = 255;// x = 0000000011111111 in binary notationx = ~x;// x is now 1111111100000000 in binary, 65280 in decimal & (Bitwise AND): The result of this operation is a value with 1s only in the bitplaces where both of its operands have/had ones (had because one of the operands can be assigned the result thanks to C''s statement chaining and operator precedence).unsigned short int x = 255;// x = 0000000011111111 in binary notationunsigned short int y = 170;// y = 0000000010101010 in binary notationunsigned short int z = x & y;// z = 0000000010101010 (1s where both x and y have 1s) | (Bitwise OR): The result of this operation is a value with 1s in bitplaces where either of its operands has a 1.[code]unsigned short int x = 255;// x = 0000000011111111 in binary notationunsigned short int y = 2733;// y = 0000101010101101 in binary notationunsigned short int z = x & y;// z = 0000101011111111 (1s where either of x and y have 1s) Hope that helped. 0 Share this post Link to post Share on other sites

Zipster 2365 Report post Posted March 5, 2003 Doesn''t ^ (XOR) count as a bitwise operator? Or did you exclude it simply because an XOR can be performed as a combination of those primary operators? 0 Share this post Link to post Share on other sites

kdogg 204 Report post Posted March 5, 2003 Just a little correction, the << operator returns the value of the variable shifted left, but doesn''t change the value of the variable. Soint x = 5;x << 2; // x still equals 5x <<= 2; // x now equals 20Same for right shift. Computers are very fast bitwise operations and slow at multiplication, division, and modulus. That''s generally why people use them. However, any compiler worth anything will automatically changex * 8;intox << 3; 0 Share this post Link to post Share on other sites

Jan Wassenberg 999 Report post Posted March 5, 2003 > Computers are very fast bitwise operations and slow at multiplicationThe P4 lacks a barrel shifter (garrggh), so it''s the other way around - muls are faster than shifts!Thanks, Intel... 0 Share this post Link to post Share on other sites

Oluseyi 2103 Report post Posted March 6, 2003 quote:Original post by Zipster Doesn''t ^ (XOR) count as a bitwise operator? Or did you exclude it simply because an XOR can be performed as a combination of those primary operators? I forgot. So, here it is:^ (Bitwise exclusive-or): This compares its operands bit-by-bit and returns a result with ones in the bit places where only one of the two operands has a one. In bitplaces where both operands have 1s, or bitplaces where both operands have 0s, exclusive-or returns a zero.unsigned short int x = 181; // x is 10110101 in binary notationunsigned short int y = 150; // y is 10010110 in binary notationunsigned short int z = x ^ y; // z is 00100011 in binary notation, 35 decimal 0 Share this post Link to post Share on other sites