Take the 2-minute tour ×

I am a c++ programmer and occasionally I'll come across some code that is using bitwise operators to manipulate things at the bit level, but I have no real understanding of those concepts. So I would like a resource to help me learn it so well that it becomes second nature. Does anyone know of good resources for this? A google search did not provide me much useful information. Maybe I'm not sure what to look for. Thanks!

share|improve this question
In high level languages, I've always missed the rotate-through-carry instructions. If I ever design a language, it'll have that. –  Nosredna Jul 22 '09 at 20:31
What kind of apps do you need that in? I'm guessing coms. –  anon Jul 22 '09 at 20:39
Well, by having it, it's easier to do multibyte shifts. Whatever you need it for. Stuff I drop down into assembly to do. Maybe instead of the shift and rotate through carry, I could do something higher level, like allow bitwise on arrays of integers. –  Nosredna Jul 22 '09 at 20:46
Come to think of it, why the heck can't I do a << or a >> on an array and have the bits move through the bytes? Why can't I do a bitwise or on two equal-sized arrays of ints in one operation? –  Nosredna Jul 22 '09 at 20:49
add comment

7 Answers

up vote 11 down vote accepted

I absolutely love Henry Warren's Hacker's Delight book. The website for it offers Chapter 2 ("Basics") as a free sample which, despite the name, offers some fairly sophisticated bit manipulation tricks. It might not be the best place to start, but it's fantastic once you start to get comfortable with bit arithmetic.

share|improve this answer
+1 for Hacker's Delight - it's an excellent book which I'm constantly referring back to for fast ways to perform all sorts of algorithms –  Dave Rigby Jul 22 '09 at 20:31
Also +1 for this great book. Though not all of the bit-twiddling is a win these days, it's still fascinating stuff. –  Andy J Buchanan Jul 22 '09 at 22:41
Although there are lots of good web links listed, this book strikes me as just what I'm looking for. Thanks! –  JimDaniel Jul 23 '09 at 14:50
man aren't there any animated videos –  Muhammad Umer Aug 9 '13 at 6:21
add comment

You'll have to know the background of the binary system. http://en.wikipedia.org/wiki/Binary_numeral_system has a great overview. Look at http://en.wikipedia.org/wiki/Bitwise_operation for bitwise operations.

share|improve this answer
add comment

I imagine there are other possibly better resources, but I remembered that there was an article on this I read a few years ago on GameDev.Net.

BitWise Operations in C

share|improve this answer
I just linked to that same one, It was in my Google Chrome bookmarks. It was a really good tutorial. –  Lucas Jul 22 '09 at 20:17
add comment

I agree with the responses above on that GameDev.net article. That was a good one for me in the past.

This article on Denormalization with Bitmasks (http://maggienelson.com/2009/02/denormalization-with-bitmasks/) was useful to me as well and includes database-specific examples, in case those are of use.

share|improve this answer
add comment

I would suggest the following tutorials:

http://www.cprogramming.com/tutorial/bitwise_operators.html http://www.codeproject.com/KB/cpp/bitbashing.aspx

They both have nice examples.

share|improve this answer
add comment

Your Answer


