Bitwise Operators

Bitwise operators that make sense

K&R gives detailed description of the behaviours of these operators but since I don’t how moving bits can be useful I have to memorise those rules. It’s only when I was reading The Linux Programming Interface that the whole thing began to make sense to me.

While most books and websites focus on behaviours, simply telling you what you will get when you apply a bitwise operator on two integers, this is a whole other story when you are dealing with the Linux/Unix programming interface, and you will often find yourself using bitwise operators to set flags rather than calculate integers. So instead of meaningless integers 5&9 you can now see bits like 0001 and 1000 as options, flags or switches.

Since these are all switches and flags, we certainly don’t want to memorise these bit patterns and therefore we give them names like

And with that we can do something like

That’s it, and I hope bitwise operations now become more natural to you (and if it still doesn’t make sense, why not just try bit-fields first). More on this can be found in this blog post.



Writing short notes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store