Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical applications of bit shifting

Tags:

c

bit-shift

bits

I totally understand how to shift bits. I've worked through numerous examples on paper and in code and don't need any help there.

I'm trying to come up with some real world examples of how bit shifting is used. Here are some examples I've been able to come up with:

  • Perhaps the most important example I could conceptualize had to do with endianness. In big endian systems, least significant bits are stored from the left, and in little endian systems, least significant bits are stored from the right. I imagine that for files and networking transmissions between systems which use opposite endian strategies, certain conversions must be made.

  • It seems certain optimizations could be made by compilers and processors when dealing with any multiplications that are n^2, n^4, etc. The bits are just being shifted to the left. (Conversly, I suppose the same would apply for division, n/2, n/4, etc.)

  • In encryption algorithms. Ie using a series of bit shifts, reverses and combinations to obfuscate something.

Are all of these accurate examples? Is there anything you would add? I've spent quite a bit of time learning about how to implement bit shifting / reordering / byte swapping and I want to know how it can be practically applied = )

like image 429
Calvin Froedge Avatar asked Feb 26 '12 18:02

Calvin Froedge


People also ask

What is a bit shift?

A bit shift moves each digit in a number's binary representation left or right. There are three main types of shifts: When shifting left, the most-significant bit is lost, and a 0 bit is inserted on the other end.

What is the bitwise right shift operator?

The bitwise right shift operator is very similar to the left shift operator. Instead of shifting the bits to the left, believe it or not, it shifts the bits to the right. Yes, you didn’t see that one coming did you?

What happens to the least significant bit when shifting right?

When shifting right with a logical right shift, the least-significant bit is lost and a 0 is inserted on the other end. For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.

How to do bitwise XOR with left shift?

Using the method demonstrated in Using the left shift we can put 1 at the position of “i” and “j”. And if we perform bitwise OR we will get the mask for ith and jth position. Perform bitwise XOR of the num with the mask and we will get our required answer with the bits flipped.


2 Answers

I would not agree that the most important example is endianness but it is useful. Your examples are valid.

Hash functions often use bitshifts as a way to get a chaotic behavior; not dissimilar to your cryptographic algorithms.

like image 98
Captain Giraffe Avatar answered Sep 28 '22 01:09

Captain Giraffe


One common use is to use an int/long as a series of flag values, that can be checked, set, and cleared by bitwise operators.

Not really widely used, but in (some) chess games the board and moves are represented with 64 bit integer values (called bitboards) so evaluating legal moves, making moves, etc. is done with bitwise operators. Lots of explanations of this on the net, but this one seems like a pretty good explanation: http://www.frayn.net/beowulf/theory.html#bitboards.

And finally, you might find that you need to count the number of bits that are set in an int/long, in some technical interviews!

like image 20
Kevin Avatar answered Sep 28 '22 01:09

Kevin