Bitshifting for dummies

Disclaimer: The writer is a dummy in bit shifting and therefore this post does not come with any warranty of replacing your job if you replace all the business calculations in your system with the methods mentioned here.

The below examples only work for int, byte, short and not on floating point values.

You may have read about bit shifting somewhere. Or perhaps your Highschool or College  teacher taught this to you or you learned it on your own. But where you able to apply it in real-world situations? In most cases, the purpose of bit shifting is to improve performance. Let's look at the two most common operators

 >> - The Right shift operator
 << - The Left shift operator

 
In applications where performance is critical, you don't only have to optimize your algorithm. You also have to optimize your code. When building a game for example, how would you code the player's speed times two? Probably like this...

player_speed *= 2;

There's nothing wrong with that. Besides, it helps readability of your code. But we can optimize it more

player_speed = player_speed << 1;

What's my basis? Well... I just know that the right shift and left shift operators are low-level operators, that's all.

You might wonder why I right shifted with 1 there. If you remember the number systems and what each bit in the binary system represents, you'll get the idea

128 64 32 16 8 4 2 1

Now, assuming player_speed is initially 4

player_speed = player_speed << 2 //this would equal 16

do you now get the idea? Here's a more detailed diagram

----------------------------------------

(7)  (6)  (5) (4) (3) (2) (1) (0)                    
128 64  32 16  8   4   2   1 

----------------------------------------

128 - 7
 64  - 6
 32 - 5
16 - 4
 8   - 3
 4 - 2
 2 - 1
 1 - 0 

----------------------------------------

So if you want to multiply by 2, you only have to remember that table above and see that to achieve the same result of multiplying a number by 2 using the multiplication operator can also be achieved using the left shift operator and that the integer literal 1 would yield the result of times two.

Use left shift(<<) if you want to perform multiplication and right shift(>>) for division.

Java Sample 

public class Main {
 
  /** Creates a new instance of Main */
  public Main() {
  }
 
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    // TODO code application logic here
    int bitShift = 4 << 1;
   
    System.out.println("Left shift by 1: " + bitShift);
   
    bitShift = bitShift >> 1;
   
    System.out.println("Right shift by 1: " + bitShift);
   
  }
 
}

 

For more detailed tutorials on bit shifting. Visit these links

http://www.javaranch.com/journal/200406/Journal200406.jsp
http://www.javaranch.com/campfire/StoryBits.jsp 

Published 10-03-2007 1:47 AM by lamia
Filed under:

Comments

Wednesday, October 03, 2007 7:20 AM by modchip

# re: Bitshifting for dummies

Wow! We also do that trick in ASM!

;supposing the content of eax is 10

shl eax, 1 ; eax = 10 * 2

Cool! :D

Wednesday, October 03, 2007 8:33 AM by lamia

# re: Bitshifting for dummies

Oh yes it is. You can do this in C/C++ btw!