Advanced Operators - Any Clue?

Fork501

Freshman
Joined
Jan 27, 2007
Messages
41
Location
Weston, Florida
Hi, all

I've been programming in C#/C++ for a few years and keep running into these weird operators (I hope I'm using the right term) that I have never found out what they are.

Here's a list of some:

|
>>=
|=

I'm working with some Home Automation products, which require me to do some programming through the COM port and can't figure out why some of the examples I found use these when working with bytes.

Here's a few examples of what I found:

Code:
BYTE hc;

hc|= 0x4;

Code:
  for (BYTE bBit = 0x80;bBit > 0;bBit >>= 1)
  {

Code:
BYTE hc;
hc = houseCodes[(BYTE(cHouseCode) | 0x20) - 'a'];

I'm a firm believer in NOT stealing code and I can't learn from this code if I can't understand what the syntaxes even mean!

Can anyone help shed some light?

The above lines of code are from an open-source VC++ project on CodeProject.

Thanks in advance!

~Derek
 
The >> is a bit shift operator - it shifts the bits of a number to the right. The >>= is just a shorthand version
i.e.
C#:
int i = 128; // 10000000
i = i >> 1;  // i now equals 64 or 01000000
i =128;
i >>= 2;    //i now equals 32 or 00100000 (same as i = i >> 2)
The << and <<= do the same but shift to the left.

The | operator is a bitwise or e.g.
C#:
int i = 128;    // 10000000
int j = 64;     // 01000000
i = i | j;      // 11000000 or 192

http://msdn2.microsoft.com/en-us/library/6a71f45d(vs.71).aspx has a bit more reading on the topic though.
 
Last edited:
The >> is a bit shift operator - it shifts the bits of a number to the right. The >>= is just a shorthand version
i.e.
C#:
int i = 128; // 10000000
i = i >> 1;  // i now equals 65 or 01000000
i =128;
i >>= 2;    //i now equals 32 or 00100000 (same as i = i >> 2)
The << and <<= do the same but shift to the left.

The | operator is a bitwise or e.g.
C#:
int i = 128;    // 10000000
int j = 64;     // 01000000
i = i | j;      // 11000000 or 192

http://msdn2.microsoft.com/en-us/library/6a71f45d(vs.71).aspx has a bit more reading on the topic though.


Thank you very much! That explains exactly why I only saw them when referring to bytes.

~Derek
 
Back
Top