#111 – Combining the Logical or Shift Operators with the Assignment Operator

To perform a logical bitwise operation on a variable and assign the result back to the original variable, you can use the following shortcut.

Instead of writing :

 x = x & 0x0020;

You can write :

 x &= 0x0020;

You can do the same thing with the other bitwise logical operators and the shift operators:

 x |= 0x2200;   // Logical OR
 x ^= 0x1A1A;   // Logical XOR
 x <<= 4;       // Left-shift x by 4 bits
 x >>= 3;       // Right-shift x by 3 bits
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: