1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Using shift operator for faster division and multiplication

Discussion in 'Programming' started by pradeep, Feb 21, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Multiplications

    12 * 2 = 12 << 1
    12 * 4 = 12 << 2
    12 * 8 = 12 << 3
    12 * 16 = 12 << 4
    12 * 32 = 12 << 5
    12 * 64 = 12 << 6
    12 * 128 = 12 << 7
    12 * 256 = 12 << 8

    Divisions

    12 / 2 = 12 >> 1
    12 / 4 = 12 >> 2
    12 / 8 = 12 >> 3
    12 / 16 = 12 >> 4
    12 / 32 = 12 >> 5
    12 / 64 = 12 >> 6
    12 / 128 = 12 >> 7
    12 / 256 = 12 >> 8


    Code:
     #include<stdio.h>
     
     void main(void)
     {
       int a;
     
       a = 12 << 1;
     
       printf("%d",a);
     }
     
     
  2. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    817
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    in machine terms,
    for i12 / 256, 12 >> 8 is faster. Is that what you mean?
    or just for easier calculations?
     
  3. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    this method does not work if you have logic one at msb
    for eg
    Code:
    // before: binary equivalent 10000000 
    uint_8 t = 128;
    t<<1;
    // after: decimal equivalent : 0
    // after: binary equivalent 00000000 
    
     

Share This Page