What Does the |= Operator Mean in C++

What does the |= operator mean in C++?

Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:

a = a | b;
a |= b;

The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.

The big advantage of the '|=' operator is when 'a' is itself a complex expression:

something[i].array[j]->bitfield |= 23;

vs:

something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;

Was that difference intentional or accidental?

...

Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:

something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;

Similar comments apply to all of the compound assignment operators:

+= -= *= /= %=
&= |= ^=
<<= >>=

Any compound operator expression:

a XX= b

is equivalent to:

a = (a) XX (b);

except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.

what does |= operator mean in C?

It works like the | + the = operator, in a similar way as += works.

It is equivalent as

a = a|b;

I suggest you to read this article about operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators
Ans this one about bitwise operation
http://en.wikipedia.org/wiki/Bitwise_operation

What does this = operator mean in C?

The expression set >>= 1; means set = set >> 1; that is right shift bits of set by 1 (self assigned form of >> bitwise right shift operator check Bitwise Shift Operators).

Suppose if set is:

BIT NUMBER    31   n=27        m=17                 0
▼ ▼ ▼ ▼
set = 0000 1111 1111 1110 0000 0000 0000 0000

Then after set >> = 1; variable set becomes:

BIT NUMBER    31   n=26        m=16                 0
▼ ▼ ▼ ▼
set = 0000 0111 1111 1111 0000 0000 0000 0000

Notice the bits number shifted.

Note a interesting point: Because set is unsigned long so this >> operation should be logical shift( unsigned shift) a logical shift does not preserve a number's sign bit.

Additionally, because you are shifting all bits to right (towards lower significant number) so one right shift is = divide number by two.

check this code (just to demonstrate last point):

int main(){
unsigned long set = 268304384UL;
set >>= 1;
printf(" set :%lu \n", set);
set = 268304384UL;
set /= 2;
printf(" set :%lu \n", set);
return 1;
}

And output:

 set :134152192 
set :134152192

(note: its doesn't means >> and / are both same)

Similarly you have operator <<= for left shift, check other available Bitwise operators and Compound assignment operators, also check section: bit expressions and difference between: signed/arithmetic shift and unsigned shift.

What does the |= Operator mean?

You know how x += 1 means x = x + 1, well x |= 1 means x = x | 1. Of course | means bitwise OR.

C++ meaning |= and &=

Also I think it should be explained what these operators do and are used this way.

m_controlState serves as flags, which means it contains in binary form which of the keys are pressed.
For example if the values of tds constants are chosed like this:

TDS_LEFT             = 0x00001
TDS_RIGH = 0x01 << 2 = 0x00010
TDS_UP = 0x01 << 3 = 0x00100
TDS_DOWN = 0x01 << 4 = 0x01000

Then in single integer you can store information which options are set. To do that you just have to check if bit that corresponds on each setting is 1 or 0.

So to set TDS_LEFT option, you have to OR the current state with 0x00001( which is TDS_LEFT), so in code

m_controlState = m_controlState | TDS_LEFT

which is the same as

m_controlState |= TDS_LEFT.

To unset TDS_LEFT option you have to AND it with ~TDS_LEFT. So

m_controlState = m_controlState & ~TDS_LEFT

which is the same as:

m_controlState &= ~TDS_LEFT

You can also check: How to use enums as flags in C++?.
Hope that makes it clearer.

What does the operator mean in C#?

Definition

The left-shift operator (<<) shifts
its first operand left by the number
of bits specified by its second
operand. The type of the second
operand must be an int.
<< Operator (MSDN C# Reference)
alt text

For binary numbers it is a bitwise operation that shifts all of the bits of its operand; every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in.

Usage

Arithmetic shifts can be useful as efficient ways of performing multiplication or division of signed integers by powers of two. Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2n. Shifting right by n bits on a two's complement signed binary number has the effect of dividing it by 2n, but it always rounds down (towards negative infinity). This is different from the way rounding is usually done in signed integer division (which rounds towards 0). This discrepancy has led to bugs in more than one compiler.

An other usage is work with color bits. Charles Petzold Foundations article "Bitmaps And Pixel Bits" shows an example of << when working with colors:

ushort pixel = (ushort)(green << 5 | blue);


Related Topics



Leave a reply



Submit