Shifting a double-wide variable - bit twiddling

In summary, the conversation discusses the efficient way to shift a double-wide variable by using bit twiddling. The 'obvious' way involves 5 operations and two assignments, but suggestions are made to reduce the number of operations. One suggestion is to replace the bit mask operation with just the variable itself, and the other is to use a bitwise OR instead of the addition operator. These suggestions aim to improve the program's execution time on various systems.
  • #1
CRGreathouse
Science Advisor
Homework Helper
2,844
0
Shifting a double-wide variable -- bit twiddling

I have a number too long to fit into the largest variable size (unsigned long long, 64 bits in this case) so I'm storing it in two variables, left and right. I want to shift it -- and to do it quickly, as I'll do this more than 1e12 times.

The 'obvious' way is

Code:
unsigned long long right, left;

. . .

right = ((left&3) << 62) + (right >> 2);
left >>= 2;

for a total of 5 operations and two assignments. This example is a right shift of 2, but right shifts by 1 and 4 are also important. In any case the size of the shift should be known at compile time.

1. Is a shift of 62 too large to be done efficiently? I know that older processors used to take O(n) time to shift n places (something like 3+n micro-ops), but I don't know if this holds on modern processors. Perhaps I should make a switch statement with constant to add (1 << 62, 2 << 62, and 3 << 62 precomputed)? Or would the branch misprediction outweigh this?
2. Is there a way to do this with fewer operations? I'm sure there are some people out there who have been doing bit manipulations far longer than I have.

For what it's worth I'm on a Core Duo, but I'm hoping for improvements to my program that work on most systems.
 
Technology news on Phys.org
  • #2
You can replace "(left&3)" with just "left" since the bit mask operation is redundant given the left shift. It cuts your cost down to 4 operations.

I would also replace the "+" operator with a bitwise OR instead. You are not adding values but combining bits. It may not actually save any execution time, but a "|" is simpler than a "+" operation so there is a remote chance that it is faster on some systems.

right >>= 2;
right |= left << 62;
left >>= 2;
 
  • #3
Ooh, thanks. That does help.
 

1. What does it mean to "shift" a double-wide variable?

Shifting a double-wide variable refers to moving the binary digits of the variable left or right by a certain number of bits. This can also be thought of as multiplying or dividing the variable by a power of two.

2. How is bit twiddling used in shifting a double-wide variable?

Bit twiddling is a term used to describe manipulating individual bits within a binary number. In shifting a double-wide variable, bit twiddling is used to move the bits to the desired location within the variable.

3. What is the purpose of shifting a double-wide variable?

Shifting a double-wide variable can be used for a variety of purposes, such as performing mathematical operations, setting or clearing specific bits, or converting between different data types.

4. Are there any limitations to shifting a double-wide variable?

Yes, there are a few limitations to consider when shifting a double-wide variable. For example, shifting too far to the left or right can result in data loss or unexpected behavior. Additionally, shifting negative numbers or using a negative shift value can produce unpredictable results.

5. What are some common mistakes to avoid when shifting a double-wide variable?

Some common mistakes to avoid include using the wrong shift operator (such as using a logical shift instead of an arithmetic shift), forgetting to handle overflow or underflow, and not considering the endianness of the system. It is important to carefully plan and test the intended shifts to ensure the desired outcome is achieved.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
Replies
19
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Calculus and Beyond Homework Help
Replies
25
Views
348
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
949
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
921
  • General Math
Replies
1
Views
1K
Replies
5
Views
2K
Back
Top