Programming languages often come with various bit operators (e.g. bitwise left- and right shift, bitwise AND, OR, XOR...). These don't get used though very much, or at least such has my experience been. They are sometimes used in programming challenges or interview questions, or the solution migh require them, e.g.:

Without using any equality operator, create a function which returns true when two values are equal

Without using a third variable, swap the value of two variables

These then again, probably have few real world uses. I guess that they should be faster because they directly manipulate memory on a low level.

Why are such found in most programming languages? Any real world use cases?

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.
If this question can be reworded to fit the rules in the help center, please edit the question.

@Anto - An easy example would be sending 256Kb worth of data at a rate of 256 words at a time ( 4096 bytes ) to a client.
–
RamhoundApr 5 '11 at 18:13

1

"Without using any equality operator, create a function which returns true when two values are equal" - in C: return !(x-y);? I dunno
–
Andrew ArnoldApr 5 '11 at 18:39

@Andrew: That is a solution, but you can do it with bitwise operators as well.
–
AntoApr 5 '11 at 18:49

19

"These don't get used though very much" - Sure about that? I use them all of the time. We don't all work in your problem domain.
–
Ed S.Apr 5 '11 at 20:06

2

Not enough for a full answer, but try reading the top 4 bits of a byte without bit fiddling and then consider that some data formats are very tightly packed.
–
Brendan LongApr 6 '11 at 1:39

+1 for your rather comprehensive list, which you even seem to be adding to
–
AntoApr 5 '11 at 18:24

27

+1. @Anto: This list is nowhere near comprehensive. A comprehensive list of use cases for bitwise operators in systems programming would be as long as a comprehensive list for SQL queries in business applications. Fun fact: I use bitwise operations all the time, but haven't written an SQL statement in years ;-)
–
nikieApr 5 '11 at 18:29

By the same line of thought, you could argue that addition has few real-world uses, since it can be replaced completely with subtraction (and negation) and multiplication. But we keep addition because it's a fundamental operation.

And don't think for a moment that just because you haven't seen much need for bitwise operations doesn't mean they're not used very often. Indeed, I've used bitwise ops in nearly every language I've used for things like bit masking.

Off the top of my head, I've used bitwise ops for image processing, bitfields and flags, text processing (e.g., all characters of a particular class often share a common bit pattern), encoding and decoding serialized data, decoding VM or CPU opcodes, and so on. Without bitwise ops, most of these tasks would require many times more complex operations to perform the task less reliably or with poorer readability.

Decoding CPU instructions for RISC-type CPUs (such as when emulating another platform) requires extracting portions of a large value as above. Sometimes, doing these operations with multiplication and division and modulo, etc., can be as much as ten times slower as the equivalent bitwise ops.

Shifting is faster than multiplying or dividing by a power of two. For example, a <<= 2 multiplies a by 4. Conversely, a >>= 2 divides a by four. One can also bit-bang data out to a device using the bit-wise operators. For example, we can send N serial data streams out of an N pin port using shift, xor, and "and" operations inside of N loops. Anything that can be accomplished in digital logic can also be accomplished on software and vise versa.

Just be careful when dividing with rounding up or down etc. Shifting does not account for that, so I've found it actually better practice to use a divide in code when I mean a divide and let the compiler optimise it into a shift and add for me.
–
DaeminApr 6 '11 at 6:24

@Daemin: I am working with integers when I use this technique. The default behavior for integer division in C and C++ is truncation toward zero; therefore, shifting an integer right by a power of two produces the same result as dividing an integer by a power of two.
–
bit-twiddlerApr 6 '11 at 21:59

1

@bit-twiddler Right shift does not work the same way as division for negative numbers.
–
DaeminApr 7 '11 at 6:24

@Daemin: You seem to be hell bent on proving me wrong. First, you throw up the rounding problem. When I repudiate that claim by stating that division in C and C++ truncates toward zero, you throw out the signed integer issue. Where did I say that I was applying the shift operator to signed two's complement negative integers? With that said, one can still use the shift operator to divide by a power of two. However, because C and C++ perform and arithmetic right shift instead of a plain old right shift, one must first check to see if value is negative. If the value is negative,
–
bit-twiddlerApr 7 '11 at 14:45

1

Exactly, be careful when using shifting as a substitute for multiplication and division as there are subtle differences. No more no less.
–
DaeminApr 8 '11 at 7:40

Long long ago, bit operators were useful. Today they are less so. Oh they aren't entirely useless, but it's been a long time since I've seen one used that should have been used.

In 1977 I was an assembly language programmer. I was convinced assembler was the only true language. I was certain that language like Pascal were for academic weenies who never had to get anything real done.

Then I read "The C Programming Language" by Kernighan and Ritchie. It changed my mind completely. The reason? It had bit operators! It was an assembly language! It just had a different syntax.

Back in those days I couldn't conceive of writing code without ands, ors, shifts, and rotates. Nowadays I almost never use them.

So, the short answer to your question is: "Nothing." But that's not quite fair. So the longer answer is: "Mostly nothing."

Bit operators are useful to this very day. The fact that in your domain they're not used does not make it unused or even not used very often. Here's a simple example: try and implement AES without bit operators. That's one off-the-cuff example of something that's done in most computers on a daily basis hundreds or thousands of times per day.
–
JUST MY correct OPINIONApr 6 '11 at 14:40

Encoding/decoding data without using bit-wise operators is painful at best. For example, adding MIME attachments to a message requires us to be able to handle three-to-four coding of data (a.k.a. radix64 coding).
–
bit-twiddlerApr 7 '11 at 15:17

I use them quite a lot in managed code (C# / .Net), and it's nothing to do with space saving, high performance or clever bit shifting algorithms. Sometimes some logic is just well suited to storing data in this way. I often use them when I have an enum but the instances can simultaneously take multiple values from that enum. I can't post an example of code from work, but a quick google for "Flags enum" ("Flags" is the C# way of defining an enum to be used in a bitwise way) gives this nice example: http://www.dotnetperls.com/enum-flags.

There is also bit parallel computing. If your data is only 1's and 0's, you can pack 64 of them into an unsigned long long word, and get 64way parallel operations. Genetic information is two bits (representing the AGCT encoding of DNA), and if you can do the various computations in bit parallel fashion you can do a lot more than if you don't. Not to mention the density of data in memory -if memory, or disk capacity, or communications bandwidth is limited implies that compression/decompression should be considered. Even low precison integers, which show up in areas like image processing, can take advantage of tricky bit parallel computing. It is a whole art unto itself.

Well that is probably because they correspond to assembly instructions and sometimes they are just useful for things in higher level languages. The same thing applies to the dreaded GOTO which corresponds to the JMP assembly instruction.

What are their uses?

Really there are just to many uses to name so I'll just give a recent, albeit highly localized, usage. I work a lot with 6502 assembly and I was working on a small application that converts memory addresses, values, compare values etc. into codes that can be used for the GameGenie device (Basically a cheat application for the NES). The codes are created by some bit manipulation.

Many programmers these days are used to computers with near infinite memory.

But some of use still program tiny microcontrollers where every bit counts (when you only have 1k or less RAM for instance), and the bitwise operators allows a programmer to use those bits one-at-a-time instead of wasting some much larger programming abstraction entity than might be needed to hold some state required by the algorithm. The IO on those devices may also require being read or controlled on a bitwise bases.

The "real world" has far far more of those tiny microcontrollers than servers or PCs.

For pure theoretical CS types, Turing machines are all about bits of state.