Home arrow C++ arrow Bitwise Operators
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.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
February 24, 2009
TABLE OF CONTENTS:
  1. · Bitwise Operators
  2. · Shift Operators
  3. · Bitwise and, or, and exclusive or
  4. · Masking Technique and Handy Tricks
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Bitwise Operators
(Page 1 of 5 )

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.

In binary system: 0 1 0 0 0 1 0 0

The n-th number: 7 6 5 4 3 2 1 0


01000100 = 2^0*0+ 2^1*0 + 2^2*1 + 2^3*0 + 2^4*0 + 2^5*0 + 2^6*1 +2^7*0

= 4 + 64 = 67.


Memorize this rule, because it will surely come in handy later. Before we venture any further, however, I shall present the operators that we have.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials