-2

My question is, what is the "|=" for in C++? I get that they are bitwise operators but i dont understand what they do here:

gObj->Variable |= 0x1000000;

Also, what does the "&" operator mean in this case?

if ((gObj->Variable & 2) == 2)
{
    do stuff
}
Peter DeWeese
  • 17,853
  • 8
  • 77
  • 100

3 Answers3

5

These are bitwise operations.

| stands for an OR operation and & stands for an AND operation.

x |= y

is equivalent to

x = x | y

It is very common to use these operations with hexadecimal values, since it is much easier and very much intuitive. For instance:

0x10 | 0x01 = 0x11
0x10 & 0x01 = 0x00
0x10 & 0x11 = 0x10
betabandido
  • 18,008
  • 10
  • 57
  • 72
3

I am no C++ expert, but I believe these are treated like += or *=. That is, it will bitwise OR the bits of that variable with the hex number you mentioned. Also, Variable & 2 is doing a bitwise AND with 10 (binary).

BlackVegetable
  • 11,724
  • 6
  • 50
  • 76
0

It is bitwise or operator and above statement will set the first bit of the variable to 1.

Peter DeWeese
  • 17,853
  • 8
  • 77
  • 100
Tariq Mehmood
  • 241
  • 1
  • 4
  • 15