Fork501 Posted March 20, 2008 Posted March 20, 2008 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 Quote Check out my blog! DevPaper.NET
Administrators PlausiblyDamp Posted March 20, 2008 Administrators Posted March 20, 2008 (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 March 21, 2008 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Fork501 Posted March 21, 2008 Author Posted March 21, 2008 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 Quote Check out my blog! DevPaper.NET
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.