Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what this operator means(|=)
used in following sample code
C#
id |= static_cast<quint32>(static_cast<quint8>(data[0] & 0x1F) << 24)


please explain this line of code. if possible..

What I have tried:

I need to know about operator, which i never used before.
Posted
Updated 24-Jun-16 6:59am
v2
Comments
Richard MacCutchan 24-Jun-16 12:39pm    
You could easily find the answer by reading your C++ reference.

Next time try Googling for the answer. This is an easy one. You just Google for "C++ |= operator". Start reading.

In this case, it's two operators in one. It's a bitwise OR operation combined with an assignment, like this:
C |= 2

is the same as
C = C | 2


In your example, the equivilent code would be:
frame2send.id = frame2send.id | (data[0] & 0xff) << 24;
 
Share this answer
 
v3
Comments
XamBEE 24-Jun-16 12:39pm    
thanks
Let's start on a more simple example, because properties do not always Support this syntax.

C++
byte theValue= 0;

theValue|= 0x01;

means the same as:
theValue= theValue | 0x01;

The remaining question is what does Operator "|" means:

It means bit whise "or" of the two arguments.
Now the question is what means "bit wise or":
0 or 0 is 0
0 or 1 is 1
1 or 0 is 1
1 or 1 is 1
You can maybe see in the table above, as soon as one of the arguments is "1" the result is "1".

Now, I assume you know a byte is 8 bits, in case not ask Google for more.
Arg1       : 0000 0000
Arg2       : 0000 0001
Arg1 | Arg2: 0000 0001
and so on
To dive more deep into this ask Google:
Bitwise operations in C - Wikipedia, the free encyclopedia[^]
 
Share this answer
 
v2
It is the same for every assignment operator. This
C++
id |= something;

means
C++
id = id | something;


C Operators[^] look at assignment operators

You should sharpen your knowledge of C/C++ basics
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
 
Share this answer
 
v3
Comments
[no name] 25-Jun-16 10:37am    
A 5. I assume the "4" was for some strange formatting issues ;)
Bruno
Patrice T 25-Jun-16 10:58am    
Thank you.
I don't see a "4", but corrected some formatting issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900