- 1
What is the use of &= and |= assignment operators in C#
Please try to explain with examples. I have done a lot of research on this and it just keeps getting harder to get. & Thanks in advance for your reply
2 Respostas
+ 5
Are you asking about how it works?
`a |= b` is the same as `a = a | b`.
Are you asking about where it is used in practice?
I have almost never used them in code. But since they're bitwise operators, you might encounter them in code that requires accessig bits..
int low_bit = 1;
int high_bit = 1 << 7;
int mask = 0;
mask |= low_bit;
mask |= high_bit;
And now you've made a bit mask that can be used to extract the highest and lowest bit of a byte at once:
int myByte = 127; // binary 1111 1111
int result = myByte & mask; // binary 1000 0001
This is I admit is a very made up example but |= and &= are just very rare in practice. If you have understood & and = though they shouldn't be to hard to grasp.
https://www.sololearn.com/learn/4073/?ref=app
0
Thanks a lot Schindlabua . I understand it better now.