Programming‎ > ‎Language‎ > ‎C++‎ > ‎

Bitwise operation

if (m_running != -1 && m_running != -1)

The C++ bitwise operators and the effect of using them on a (0110) and b (0101).

Symbol   Name            C++       Result
<<       Left shift      a << 1    1100   
   
>>       Right shift     a >> 1    0011
~        NOT             ~a        1001
&        AND             a & b     0100
|        OR              a | b     0111
^        XOR             a ^ b     0011

How to use them
number = number << X;         // Shift the bits left x step.
number = number >> X;         // Shift the bits right x step.
bitmask = (1 << x);           // Create a bitmask with the x bit from the right set.
bit = number & (1 << x)       // Check a bit.

number |= (1 << X);           // Set a bit.
number &= ~(1 << x);          // Clear a bit.
number ^= (1 << x);           // Toggle a bit.

Bit Twiddling - 2006

Comments