Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the |= Operator mean?

Tags:

c#

In some code i found this |= operator used to return a uint but i can find something about it on the internet and i wanna understand how it works and what are the return values in this case.

public uint Mask
{
    get
    {
        uint num = 0;
        if (_0)
            num |= 1U;
        if (_1)
            num |= 2U;
        if (_2)
            num |= 4U;
        return num;
    }
}

a detailed answer will be much appreciated.

like image 898
Daniel Eugen Avatar asked Nov 15 '16 09:11

Daniel Eugen


People also ask

What is the meaning of |= operator?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What does |= mean in programming?

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.

How does |= operator in C works?

This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. For example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8. Then (a -= 6) = 2.

What does |= mean in Python?

For |= , think of the known examples, x +=5 . It means x = x + 5, therefore if we have x |= 5 , it means x = x bitwiseor with 5 .


1 Answers

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

like image 99
LordWilmore Avatar answered Oct 22 '22 06:10

LordWilmore