Skip to main content
gamedev.net
🔒 Locked

Using bitwise operators

Started by Chocoboko Mar 4, 2003 at 11:17 PM 5 replies 450+ views
Original Post
Chocoboko
Chocoboko
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.
Zipster
Zipster
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 (102). Shift a base-10 number to the right a single place, and it has the effect of dividing the number by 10 (101).

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 28, or 256.

[edited by - Zipster on March 5, 2003 3:40:59 AM]
Oluseyi
Oluseyi
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 notation
    x << 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 2n (verify: 255 x 25 = 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 notation
    x >> 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 notation
    x = ~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 notation
    unsigned short int y = 170;
    // y = 0000000010101010 in binary notation
    unsigned 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 notation
    unsigned short int y = 2733;
    // y = 0000101010101101 in binary notation
    unsigned short int z = x & y;
    // z = 0000101011111111 (1s where either of x and y have 1s)


Hope that helped.
Zipster
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?
kdogg
kdogg

Just a little correction, the << operator returns the value of the variable shifted left, but doesn''t change the value of the variable. So

int x = 5;

x << 2; // x still equals 5
x <<= 2; // x now equals 20

Same 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 change

x * 8;
into
x << 3;
Jan Wassenberg
Jan Wassenberg
> Computers are very fast bitwise operations and slow at multiplication
The P4 lacks a barrel shifter (garrggh), so it''s the other way around - muls are faster than shifts!
Thanks, Intel...
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Oluseyi
Oluseyi
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 notation
    unsigned short int y = 150; // y is 10010110 in binary notation
    unsigned short int z = x ^ y; // z is 00100011 in binary notation, 35 decimal

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.