Wednesday, January 14, 2009

C++ Operators

A friend of mine recently asked me about bitwise operators and was a bit confused by them. In this post, I will break down a couple and leave you with an assignment that you can reply to in the comment section. This is pretty basic stuff but if you're not used to it, it can look alien.

The ^, is the exclusive OR (XOR) bitwise operator. The |, is the Inclusive OR bitwise operator. They manage bits at the lowest level.

Let's say you were comparing two bits and EITHER of them is 1, then an OR operation on them would yield 1. If both are 0, then the OR operation will return 0, or false. Think about it in real life... you are happy with either a PS3 OR an XBox 360... but this is not true if you had neither. And it is still true if you own both.

Now XOR, ^, is a little more picky, it requires ONLY one of the bits to be 1, or true, or ON... (the terminology depends on the context).

Here are the charts for these operators:

OR
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

XOR
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

ASSIGNMENT: The bitwise operator for AND is &. Create the chart for AND using common sense and standard knowledge. BTW, this is the core of all electronics and programming and this is what distinguishes a n00b from a real programmer haha

http://msdn.microsoft.com/en-us/library/x04xhy0h(VS.71).aspx
http://msdn.microsoft.com/en-us/library/4ke0e88k(VS.71).aspx

0 comments: