Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the /= operator in C# do?

What does the /= operator in C# do and when is it used?

like image 304
Alex Avatar asked Aug 20 '09 21:08

Alex


People also ask

What does ~= mean in C?

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.

How does |= operator in C works?

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.


3 Answers

It's divide-and-assign. x /= n is logically equivalent to x = x / n.

like image 89
chaos Avatar answered Oct 25 '22 15:10

chaos


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.

like image 44
womp Avatar answered Oct 25 '22 17:10

womp


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;
like image 4
Jonathan Leffler Avatar answered Oct 25 '22 15:10

Jonathan Leffler