Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

BYTE hc;

hc|= 0x4;

 

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

 

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

Check out my blog! DevPaper.NET
  • Administrators
Posted (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.

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.

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.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
The >> is a bit shift operator - it shifts the bits of a number to the right. The >>= is just a shorthand version

i.e.

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.

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

Check out my blog! DevPaper.NET

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...