Bitwise Operation - Applications

Applications

Bitwise operations are necessary particularly in lower-level programming such as writing device drivers, low-level graphics, communications protocol packet assembly, and decoding.

Although machines often have efficient built-in instructions for performing arithmetic and logical operations, in fact, all these operations can be performed by combining the bitwise operators and zero-testing in various ways.

For example, here is a pseudocode example showing how to multiply two arbitrary integers a and b (a greater than b) using only bitshifts and addition:

c := 0 while b ≠ 0 if (b and 1) ≠ 0 c := c + a left shift a by 1 right shift b by 1 return c

This implementation of ancient Egyptian multiplication, like most multiplication algorithms, involves bitshifts. In turn, even addition can be written using just bitshifts and zero-testing:

c := b and a while a ≠ 0 c := b and a b := b xor a left shift c by 1 a := c return b

Read more about this topic:  Bitwise Operation