Q:

What are the advantages of using bitwise operations?

Following reading the latest CodeProject newsletter, I came across this article on bitwise operations.  It makes for interesting reading, and I can certainly see the benefit of checking if an integer is even or odd, but testing if the n-th bit is set?  What can possibly be the advantages of this?

A:

Bitwise operations are absolutely essential when programming hardware registers in embedded systems.  For example every processor that I have ever used has one or more registers (usually a specific memory address) that control whether an interrupt is enabled or disabled.  To allow an interrupt to fire the usual process is to set the enable bit for that one interrupt type while, most importantly, not modifying any of the other bits in the register.
When an interrupt fires it typically sets a bit in a status register so that a single service routine can determine the precise reason for the interrupt.  Testing the individual bits allows for a fast decode of the interrupt source.
In many embedded systems the total RAM available may be 64, 128 or 256 BYTES (that is Bytes not kilobytes or megabytes)  In this environment it is common to use one byte to store multiple data items, boolean flags etc. and then use bit operations to set and read these.
I have, for a number of years been working with a satellite communications system where the message payload is 10.5 bytes.  To make the best use of this data packet the information must be packed into the data block without leaving any unused bits between the fields.  This means making extensive use of bitwise and shift operators to take the information values and pack them into the payload being transmitted.

A:

Basically, you use them due to size and speed considerations. Bitwise operations are incredibly simple and thus usually faster than arithmetic operations. For example to get the green portion of an rgb value, the arithmetic approach is (rgb / 256) % 256. With bitwise operations you would do something as (rgb >> 8) & 0xFF. The latter is significantly faster and once you're used to it, its also easier. Generally bitwise operations come into play a lot when you need to encode/decode data in a compact and fast way.

A:

These kind of operations are often used when writing for embedded systems where memory or CPU power is restricted.
For example, to save space you may store multiple variables in a single 8-bit int variable by using each bit to represent a boolean. Then you need a fast way to set a specific bit or retrieve the bit value.
Generally when programming in higher level languages like C# on a desktop PC with Gigabytes of memory, you don't really care that each bool takes up a whole byte. But if you're programming a microcontroller in C with 2kb of memory, every single bit counts, so the ability to pack 8 bools into a single byte may be critical.

