Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are bitwise operations still practical?

Wikipedia, the one true source of knowledge, states:

On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication).

Is there a practical reason to learn bitwise operation hacks or it is now just something you learn for theory and curiosity?

like image 314
CamelCamelCamel Avatar asked Jul 02 '11 21:07

CamelCamelCamel


People also ask

What is the practical use of bitwise operator?

Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines. A bitwise operator works with the binary representation of a number rather than that number's value.

Are bitwise operations important?

The Bitwise Algorithms is used to perform operations at the bit-level or to manipulate bits in different ways. The bitwise operations are found to be much faster and are sometimes used to improve the efficiency of a program. For example: To check if a number is even or odd.

Is bitwise or faster than logical or?

No. First, using bitwise operators in contrast to logical operators is prone to error (e.g., doing a right-shift by 1 is NOT equivalent to multiplying by two). Second, the performance benefit is negligible (if any).

Are bitwise operations faster than arithmetic?

Bitwise operations are incredibly simple and thus usually faster than arithmetic operations.


2 Answers

Bitwise operations are worth studying because they have many applications. It is not their main use to substitute arithmetic operations. Cryptography, computer graphics, hash functions, compression algorithms, and network protocols are just some examples where bitwise operations are extremely useful.

The lines you quoted from the Wikipedia article just tried to give some clues about the speed of bitwise operations. Unfortunately the article fails to provide some good examples of applications.

like image 128
Mackie Messer Avatar answered Sep 20 '22 15:09

Mackie Messer


Bitwise operations are still useful. For instance, they can be used to create "flags" using a single variable, and save on the number of variables you would use to indicate various conditions. Concerning performance on arithmetic operations, it is better to leave the compiler do the optimization (unless you are some sort of guru).

like image 21
Cratylus Avatar answered Sep 20 '22 15:09

Cratylus