What does the /= operator in C# do and when is it used?
The ~ operator in C++ (and other C-like languages like C and Java) performs a bitwise NOT operation - all the 1 bits in the operand are set to 0 and all the 0 bits in the operand are set to 1. In other words, it creates the complement of the original number.
The ' |= ' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.
It's divide-and-assign. x /= n
is logically equivalent to x = x / n
.
It is similar to +=
, -=
or *=
. It's a shortcut for a mathematical division operation with an assignment. Instead of doing
x = x / 10;
You can get the same result by doing
x /= 10;
It assigns the result to the original variable after the operation has taken place.
In most languages inspired by C, the answer is: divide and assign. That is:
a /= b;
is a short-hand for:
a = a / b;
The LHS (a
in my example) is evaluated once. This matters when the LHS is complex, such as an element from an array of structures:
x[i].pqr /= 3;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With