Results 1 to 5 of 5
Thread: Multiply, or shift bits?
 03152011, 06:48 PM #1Member
 Join Date
 Aug 2010
 Posts
 17
 Rep Power
 0
Multiply, or shift bits?
Hey, just a quicky!
Let's say I have a program that needs to be fast, and let's say that I want to multiply/divide something with a power of two (2^1, 2^2, ...). Would it be faster to carry out the multiplication, for example n * 2, or would it be faster to shift the bits one step to the left, n << 1? Or makes it no difference?
PLMLast edited by plmpusik; 03152011 at 06:54 PM.
 03152011, 07:18 PM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
I have no idea what the solution is. But I'll post anyway... Because the solution is easy enough to find: compile your program twice using both approaches, test and time.
The JLS just says (15.19 Shift Operators) "The value of n<<s is n leftshifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s". And I don't think the JVM specification says anything about a difference in implementation on which you can rely.
In practice I would write the expression in whatever form seemed most natural. This is subjective, of course, and what "natural" means would depend on the result of testing your and similar code.
 03152011, 07:36 PM #3Senior Member
 Join Date
 Nov 2010
 Posts
 210
 Rep Power
 6
The difference seems to be negligible. I tested it with this:
Java Code:public class ShiftVsMultiply { public static void main(String[] args) { long t = System.currentTimeMillis(); int j; for (int i = 0; i <= 500000000; i++) { j = i * 2; } System.out.println("Time taken for *2: " + (System.currentTimeMillis()  t) + "ms"); t = System.currentTimeMillis(); for (int i = 0; i <= 500000000; i++) { j = i << 1; } System.out.println("Time taken for <<1: " + (System.currentTimeMillis()  t) + "ms"); } }
Java Code:Time taken for *2: 379ms Time taken for <<1: 373ms
Last edited by Iron Lion; 03152011 at 07:39 PM.
 03152011, 08:05 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,128
 Blog Entries
 7
 Rep Power
 24
 03152011, 11:08 PM #5Member
 Join Date
 Aug 2010
 Posts
 17
 Rep Power
 0
I didn't know about this, but it makes sense when you think about it.
Wouldn't it be better if the compiler replaced our power of 2 multiplications/divisions with bit shifts and our modulus with the bitwise AND operator? Considering they actually are slightly faster.
PLM
Similar Threads

Bitshifting doesn't shift bits?
By Skiller in forum New To JavaReplies: 19Last Post: 03022011, 10:30 AM 
matrix multiply
By slixtrix in forum New To JavaReplies: 8Last Post: 09132010, 06:50 AM 
multiply two matrixes
By smart princess in forum New To JavaReplies: 8Last Post: 12062009, 07:43 PM 
display column value multiply with 100
By tiiim83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01152009, 04:40 AM 
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:50 PM
Bookmarks