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.

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?

share|improve this question

closed as too broad by MichaelT, gnat, World Engineer Apr 8 at 0:15

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.

6 Answers 6

up vote 23 down vote accepted

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.

share|improve this answer
4  
For those interested in more boolean bit operations and optimizations, have a look at Hacker's Delight: amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201914654 –  gablin Oct 22 '10 at 18:25

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.

share|improve this answer
1  
BYTE g1 = (rgb / 256) % 256; 00E51013...C1 E9 08...shr ecx,8 00E51016...88 0C 24...mov byte ptr [esp],cl –  rwong Oct 24 '10 at 9:19

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.

share|improve this answer
    
.NET has the [Flags] attribute which allows using an Enum as a bit field. For instance, Font has a Style property which is a bit field containing bold, italic, underline, and strikethrough. –  deltreme Oct 22 '10 at 10:31
    
@deltreme. Yeah I know you can use [Flags], but that's not the point. The point is that in high level languages you don't care much about the using a few bytes of space for a couple of bools. You generally care more about maintainability and code readability. In embedded systems where you do care about size, you don't have access to things like the .net [Flags] attribute, so that is where you might use these bitwise operations. –  Simon P Stevens Oct 22 '10 at 10:42
    
"but testing if the n-th bit is set? What can possibly be the advantages of this?" so it is the point - but since no language was specified I decided to enter it as a comment on an answer where C# was mentioned. It's not a comment on your answer, it's an addition, or nice-to-know, or whatever. –  deltreme Oct 25 '10 at 7:09
    
@deltreme: Oh, cool. –  Simon P Stevens Oct 25 '10 at 8:16

Bitwise operations are also used frequently in video and audio codecs, for the same reason as in embedded electronics; being able to pack five flags and an eleven bit timer into half an int is very useful when you want to make a super-efficient video codec.

In fact, MPEG 4 even uses exponential Golomb encoding for variable-bit-length fields. A value that was 17 or 19 bits wide last packet might be only three or five bits wide this packet - and you'd figure all that out with bitwise operations.

share|improve this answer

Tricks that combine bitwise logical operations, bitwise shift operations and arithmetic operations can be understood by people who have studied the construction of a binary adder using logic gates (and, or, not). Outside that circle, it is very difficult to understand without a detailed comment.

It is useful when programming SIMD units, especially if the CPU's architecture intentionally left out some SIMD instructions because they could be emulated by a few others.

For example, the architecture may not define any instructions for taking the negative values of a group of 16 bytes, but that can be emulated by bitwise negating and then adding 1. Likewise, subtraction can be omitted too, because it can be emulated by taking the negative of the second operand. The availability of the "alternate route" is the reason for omitting certain instructions.

Likewise, the SIMD may only support parallel 8-bit addition, without implementing addition for wider elements such as 16-bit, 32-bit or 64-bit. To emulate them, one needs to extract the sign bit from the 8-bit calculation result, then perform the carry operation on the next element.

share|improve this answer

Packing data, quicker ops (multiplication, division, and modulus are significantly faster if aligned to powers of 2), bit flipping, etc. Learn them and start using them and you'll slowly start to see most of the advantages on your own.

share|improve this answer
1  
That's a rather orthogonal answer. –  itsbruce Nov 12 '13 at 11:15

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