Bitwise Operators in Action
(Page 1 of 4 )
Whether we like it or not, our society is money-oriented. Earning money takes time, and time has its limits; none of us has more of it. So if you want to maximize what you're earning, you must improve your speed. Bitwise operators may not always be the best choice for this, but in some cases relying on them will pay off quite handsomely.
In this article, which by the way is the second part of a two-part series that began with an article on bitwise operators, I’ll show you why, where, and how to use them, so you’ll get the most out of them. If you missed the first part, please seek it out; it was published here on ASP Free just last week under the name "Bitwise Operators." The information presented there is necessary to comprehending this article. I’ll presume familiarity with that article already.
To start with, I’ll present the benefits and drawbacks of the bitwise operators, followed by their usage in programming generally, and summing up with their role in algorithms. So if you want to know when to use bitwise operators and what benefits you’ll get, then just read on and enjoy the ride.
When, Where, Why?
The most important property of bitwise operators is that they are executed really fast, mainly because they allow such low-level access to the memory. From this comes one of their main uses, in otherwise slow algorithms. Speeding up every little operation in an algorithm with bitwise operators can result in its running twice as fast -- or even faster.
The only glitch in this is that many operators are aware of this, and automatically transform an operation like dividing with 8 to shifting with 3. Also, some argue that by using bitwise operators, the code snippet will become a little overcomplicated and not everybody can follow it, so it brings more disadvantage than benefit.
And we didn’t even mention what we discussed in the first part of this two-part article series, that “right shifting” can be a dangerous game to play; its implementation is device-dependent, and because of this, your code will become less portable. This is a potentially serious drawback; nowadays, cross-platform compatibility is becoming more of a general requirement to target a larger segment of the marketplace.
Next: Do You Need Them? >>
More C++ Articles
More By Gabor Bernat