Click here to Skip to main content
15,121,741 members
Articles / Security / Encryption
Tip/Trick
Posted 17 Jul 2012

Stats

18.7K views
7 bookmarked

Faster Bit Rotation

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
17 Jul 2012Public Domain4 min read
Here I present an improved version of the Circular Shift Operations

Introduction

The Circular Shift is a very commonly used operation used heavily in encryption algorithms and ciphers. For the most part, it has been implemented in code, however recently some processors have started having it implemented in the hardware. Here I would like to present an improved version of the algorithm, which is faster in code, and will almost certainly be much faster in hardware.

Using the Code

Okay, I'm just going to jump into the code, and then explain afterwards how it works, although I hope the more astute of you will figure it out before I do. Smile | :)

First, let's look at the classical Circular Shifts:

C++
#include <stdint.h>

uint32_t ClassicRotateLeft(uint32_t, int);
uint32_t ClassicRotateRight(uint32_t, int);

uint32_t ClassicRotateLeft(uint32_t N, int S)
{
    return (N >> (32 - S)) | (N << S);
}

uint32_t ClassicRotateRight(uint32_t N, int S)
{
    return (N << (32 - S)) | (N >> S);
}

I really don't think I need to explain these. There are thousands of videos and Java applets available online that can explain this much better than I can. Not to mention it's pretty self explanatory.

Now to the juicy parts. Here are the new Circular Shifts / Bit Rotations:

C++
#include <stdint.h>

uint32_t NewRotateLeft(uint32_t, int);
uint32_t NewRotateRight(uint32_t, int);

uint32_t NewRotateLeft(uint32_t N, int S)
{
    return ((N >> (~S)) >> 1) | (N << S);
}

uint32_t NewRotateRight(uint32_t N, int S)
{
    return ((N << (~S)) << 1) | (N >> S);
}

First, before you continue reading, I'd ask that you try to figure it out on your own. It's actually pretty simple, and the mental exercise is quite fun.

How It Works

So the first thing you will notice is that I got rid of the (32 - S). It's uses no arithemtic operations, only bit manipulations. Anyone who has studied low level code optimizations will know that bitwise operators are much faster than complicated operations such as multiplication and division, and are a little bit faster than simpler operations such as addition and subtraction.

The only part that's different from the Classical Rotations are these:

C++
(N >> (~S)) >> 1)
(N << (~S)) << 1) 

This is the part that replaces (32 - S). It uses a simple bit trick to replace the subtraction.

So, let's say we want to Rotate an integer N by 21 bits. Again, the (N << S) & (N >> S) are the same, so I'll ignore them. Let's look at 21 as a series of 5 bits.

C++
10101 /* 21 in binary */ 

So, in the code above, we NOT 21 (flip the bits), which gives us:

C++
01010 /* 10 in binary */

By flipping the bits of 21, it gives us 10. If we add 21 and 10 we get 31. This is the same for every other 5 bit number.

C++
/* For 5 bit numbers */

N + (~N) = 31 
OR
N | (~N) = 31 

So by flipping the bits, we get 1 less than if we subtract (32 - S). So technically, we could use:

C++
(N >> ((~S) + 1))
(N << ((~S) + 1))

But, instead of using an addition instruction (and thereby throwing out optimization out the window), we could just use an extra shift to replace the one we know needs to be there.

So technically this:

C++
(N >> (~S)) >> 1)
(N << (~S)) << 1)  

is the same as this:

C++
(N >> ((31 - S) + 1))
(N << ((31 - S) + 1))

With just a tad more awesome.

How Much Faster?

1.21 giga Okay, in practice it really isn't that much faster. I had to do several trials runs with a larger and larger number of iterations each time before I noticed a significant difference. Running it at 10,000,000,000 (ten billion) iterations, there was a 1.2 second difference between the two. So yeah, don't call the press just yet. This is more of a fun little trick rather than the next stage of computing, but I do think that it still has some value. It's certainly much more efficient to implement this in hardware than the original one. And seeing as how the Circular Shifts are standard functions that are rarely changed (this being the exception), there's no reason not to use the one that is technically (albeit imperceptibly) faster.

Conclusion

Again, this is really more of a cool trick to show off rather than a huge achievement. I do think that this shows that even the most basic, standard operations can still be optimized. By itself, it's just a small change. But you never know; keep looking, and by optimizing a few more functions, those little changes can add up to make a big difference Smile | :)

Lastly, to answer what is undoubtedly going to be a common question:

Yes, when you NOT a 32 bit variable, ALL 32 bits get flipped. In example:

C++
uint32_t N = 21;
N = (~N);
/*  (N == 4294967274)  */ 

Flipping 21 gives you 4,294,967,274.

However the processor will only look at the bits significant to the operation, which means for a 32 bit the first 5 bits, 64 bit the first 6, etc. Don't worry, the processor will handle it.

Well, thanks for taking the time to look at this. Please let me know what you think about this trick, as well as your experience using it.

Jacob

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Jacob F. W.
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 Pin
dgendreau18-Jul-12 9:00
Memberdgendreau18-Jul-12 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.