Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of <<= and |=

Tags:

What is the meaning of <<= and |= in C?

I recognise << is bitshift etc. but I don't know what these are in combination.

like image 418
SK9 Avatar asked May 26 '11 06:05

SK9


People also ask

What is |= mean?

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 |= in Java mean?

operator. ~ is bitwise complement bits, 0 to 1 and 1 to 0 (Unary operator) but ~= not an operator. Additionally, ! Called Logical NOT Operator, but != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

What is |= in C programming?

|= is analogous to operators like += and -= in that it will perform a bitwise OR on the two operands then store the result in the left operator.

What does <<= mean in C #?

Since >> is the binary right-shift operator, it means to shift the value in set right by 1 bit.


1 Answers

Just as x += 5 means x = x + 5, so does x <<= 5 mean x = x << 5.

Same goes for |. This is a bitwise or, so x |= 8 would mean x = x | 8.

Here is an example to clarify:

int x = 1; x <<= 2;         // x = x << 2; printf("%d", x); // prints 4 (0b001 becomes 0b100)  int y = 15; y |= 8;          // y = y | 8; printf("%d", y); // prints 15, since (0b1111 | 0b1000 is 0b1111) 
like image 169
Chris Cooper Avatar answered Oct 15 '22 13:10

Chris Cooper