The op= Use of Bitwise Operators - C Operator

C examples for Operator:Bit Operator

Introduction

You can use all of the binary bitwise operators in the op= form of assignment.

The exception is the operator ~, which is a unary operator.

lhs op= rhs;

is equivalent to the statement:

lhs = lhs op (rhs);

This means that if you write:

value <<= 4;

the effect is to shift the contents of the integer variable, value, left four bit positions.

It's exactly the same as the following:

value = value << 4;

You can do the same kind of thing with the other binary operators.

For example, you could write the following statement:

value &= 0xFF;

where value is an integer variable. This is equivalent to the following:

value = value & 0xFF;

The effect of this is to keep the rightmost eight bits unchanged and to set all the others to 0.


Related Tutorials