C++

Bitwise Operators

Programming machines has always been a difficult task; newer techniques try to solve this by automating the tougher tasks, the low-level handling of the machines. However, some gates have been left open to the older methods, so whenever you wish to use them you are able to do so. At least this applies to C/C++. In this article you'll see how to use bitwise operators in C/C++, and learn why you may want to use them.

Many times, you don't really care how an operation is carried out as long as it is done right, so why should you struggle with the details? Well, you shouldn't, as long as it wouldn't be profitable to do so. Bitwise operators are one of those extras in C/C++. They allow you to perform some operations directly on the bytes of the integer types.

Throughout this article I'll present to you the operators that you can apply for an integer type, their effect, and the appropriate ways to use them. A second article that comes shortly after this will then cover where you can use them, and most importantly, why to use them.

You may hear proverbs throughout your life along the lines of "small but strong." You'll find this to be especially true with bitwise operators.

Thanks for tuning in, and let's begin.

The Operators

When you look up the word "bit" in the dictionary, you'll came across a definition close to this: "a tiny amount." Now in programming, a bit is the smallest amount of partially individual memory segment you can have. It is smallest in the sense that you can't divide it any further, simply because a bit can have as its value 0 or 1, and partially because in everyday usage only a byte can be used at once. A byte is equal to a sequence of 8 bits. Imagine it as something like this in memory:

If you are searching the help file for a bit type, you probably will find something close to nothing. Simply put, there is no such thing. Every single operator has its effect on a full byte at least. Note that this doesn't mean that modification of a single bit can't be done; it's just that we need to be smart and find a way to do so. That's right!

From the construction scheme of memory shown earlier comes the point that each number in our computers is represented in the binary system. And also a number can be translated from the upper sequence into the decimal system by the following rule.