Thread: shift operator

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    shift operator

    I always heard people saying to divide by 2 right shift by 1 and so on and to multiply left shift by 1 and so on. But is so wrong because if we divide 5 you will only get 2 but not 2.5 and it is applicable to only to even numbers. Then why the above statement is said?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    I always heard people saying to divide by 2 right shift by 1 and so on and to multiply left shift by 1 and so on. But is so wrong because if we divide 5 you will only get 2 but not 2.5 and it is applicable to only to even numbers. Then why the above statement is said?
    The unsigned integer 5 in binary representation is 101. If you right shift by 1, the result is binary 10, which in decimal is 2. If you were working with fractional values instead, then right shifting by 1 would result in binary 10.1, which in decimal is 2.5. But the bitwise shift operators in C only work with integer operands, so this is not possible with them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    At the lowest level of optimisation, every modern compiler I've looked at produces exactly the same code for n / 2 and n >> 1 (and other divisions commonly done using shifts) where n is an integer anyway, so sacrifice readability to use something that you have to be careful to use to avoid implementation defined behaviour that will end up with the same result anyway without you having to worry about which cases are implementation defined (e.g. if n is a negative integer). The compiler will take care of it and optimise n / 2 to n >> 1 if it can.

    Many years ago there was some justification for it, but even then you have to keep in mind that "pre-mature optimisation is the root of all evil". There are cases where using bitshifts are perfectly acceptable (or even more readable), but to divide by 2 is not (way more often than not) one of those cases.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    >if we divide 5 you will only get 2 but not 2.5

    Try to make the code so it will work with div/mult by 2/4/8/16
    and you or the compiler will use right/left shift.

    if you use numbers like *3 or *5 the compiler will most likely still just use 1 or 2 left shifts + original value.

    div by 5, as integer don't have decimal point the result will be floored.
    if you need decimal point, 32bit fixed point by Union could do it.
    Code:
    union{
     unsigned long myfixvar;
       struct{
       unsigned int myfixlow;
       unsigned int myfixhigh;
       }
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shift operator doubt
    By thannara123 in forum C Programming
    Replies: 3
    Last Post: 06-19-2015, 08:14 AM
  2. Left Shift Operator Over Loading <<
    By nickman in forum C++ Programming
    Replies: 8
    Last Post: 09-01-2014, 03:47 AM
  3. Bitwise Operator Shift
    By YannB in forum C Programming
    Replies: 14
    Last Post: 03-04-2014, 09:49 PM
  4. unsigned right shift operator
    By abraham2119 in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 11:01 AM
  5. left shift operator!!!!
    By theblackhat in forum C Programming
    Replies: 2
    Last Post: 10-02-2004, 02:07 AM