-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
  • 18,141
  • 8
  • 79
  • 101

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,946
  • 11
  • 62
  • 76
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
  • 12,594
  • 8
  • 50
  • 82
0

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

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Tariq Mehmood
  • 259
  • 1
  • 5
  • 15