Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by gnat, MichaelT, Bart van Ingen Schenau, GlenH7, World Engineer Apr 8 at 0:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
ok, so you have the colours 0xFFC140 and 0x80C020. Your challenge: interpolate correctly without bitwise operators :P –  Chris Burt-Brown Oct 5 '11 at 11:00

5 Answers 5

up vote 28 down vote accepted

"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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
4  
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. –  Michael Kjörling Oct 5 '11 at 8:09
3  
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 '11 at 8:58
    
Michael, thanks for the catch :) –  Steve Bennett Nov 30 '11 at 6:26

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.