# What is the benefit of studying bitwise operators? [duplicate]

What is the benefit of studying bitwise operators (Bitwise Not, Bitwise AND, Bitwise OR, Bitwise XOR, Left Shift, Signed Right Shift, Unsigned Right Shift etc.)?

Will we really use these operators in programming?

• ok, so you have the colours 0xFFC140 and 0x80C020. Your challenge: interpolate correctly without bitwise operators :P Oct 5, 2011 at 11:00

"Yes, we will."

Bitwise operations are everywhere. They are perfect for working with bitfields (a practice that is ubiquitous in C and C++), such as a 'flags' field in a data structure or function argument. Basically, `|` combines flags, `^` flips flags, `&` checks if a flag is set, and the `x &= ~FLAG` pattern clears a flag.

Bitwise operations are ubiquitous in all things low-level - hardware drivers, network protocols, binary file formats - as well as some higher-level fields like character encodings, cryptography, etc.

Bit-shifting can also sometimes double for integer division and multiplication by powers of 2, with a slightly different rounding behavior for negative numbers (sometimes, but not always, more desirable than what regular integer division does).

In tight loops, bitwise arithmetic can sometimes be used to avoid conditionals, which is beneficial because modern CPUs use branch prediction, and a misprediction (i.e., the condition in an `if` statement evaluates differently from the previous time) causes a significant delay. Using bitwise arithmetic, the same calculation can sometimes be expressed without any conditionals.

Even if you don't intend to work in any of the above scenarios, it is still a good idea to study and understand bitwise operations - all modern computers are binary, and you definitely need to know the basic principles by which they operate. Numbers in a computer don't behave like numbers in the real world, and studying binary operations will help you understand why.

• A very well-written, succinct answer. Thanks for this! Nov 1, 2016 at 7:05

It all depends on what your focus is really. Some programmers may go their entire career without using bitwise operations and may do quite well for themselves (although they will encounter frustration whenever dealing with code that contains them).

However, bitwise operations are a fundamental part of software engineering and questions pertaining to them are asked quite frequently in interviews (along with other fundamental CS questions such as how to convert between base-`x` and base-`y`).

You'll find that those who come from a CS background will use them when the need arises in many different ways (a common use case is a bit field). If you don't understand how bit operations work, you'll generally be at a loss trying to understand the sections containing them.

Now, having said all that, bitwise operations should be fairly easy to understand for anyone pursuing a career in programming and shouldn't really require a lot of studying.

I work in the embedded field and being completely at home with bitwise operations is a sine qua non for anyone that wants to work here. It is one of the first interview questions that I ask. All of our software has a very intimate relationship with the hardware most of which is controlled by modifying single bits in hardware registers. Changing the wrong bit has disastrous consequences.

Not much. They occasionally turn up in APIs (C/C++ particularly) that want a set of flags to be passed as a single number. But even there they're treated just like arithmetic operators (64+1 or 2<<6 | 2<<0, same diff) So if you're only working with more modern languages, they're very low priority in the pile of things you might want to learn.

• Um, you do realize that 2<<0 == 2, not 1, right? Same for 2<<6, which is 128, not 64. So the two expressions `64+1` and `2<<6 | 2<<0` are most definitely not interchangeable.
– user
Oct 5, 2011 at 8:09
• its got nothing to do with how modern the language is. e.g. I've used bit twiddling in C# to implement a particular wire protocol, while you could do it with arithmetic functions instead this would needlessly obfuscate the solution
– jk.
Oct 5, 2011 at 8:58

Here's an example of needing bitwise operations in a "normal" (non-gaming, non-low-level) application, from just today:

We have a VB.Net app that was upgraded from an old VB6 app, which allows users to pick their own color. We were having issues because the color is stored in the database as a VB6 color, which are four-byte integers stored as ABGR (alpha-blue-green-red). However, .Net uses the more usual ARGB (alpha-red-green-blue).

If you have a solid grasp of bitwise operators, this is an easy 3-minute fix. If not, however, you would likely have found yourself struggling for a good day trying to figure them out.