1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Bit shifting and bitwise operators

  1. Jun 23, 2008 #1
    1. The problem statement, all variables and given/known data
    folks,

    I have a small problem understanding a function as to what its doing:

    I have run this program in C++. I will comment the lines of code as per my understanding. Your insight would be useful

    unsigned int myfunc(unsigned int n)
    {
    // for n here I took 1200

    unsigned int temp =
    //logical shift done here as it is unsigned, division for 1200 came to 0 as it was rounded
    (n >> 24) |

    // logical shift as well, logical and operation with hex value, result is 262144
    ((n << 8) & 0x00FF0000) |

    // logical shift as well, logical and operation with hex value, came to 0 as it is rounded
    ((n >> 8) & 0x0000FF00) |

    // computes to 2952790016
    (n << 24);

    //temp is then the sum of all operations which in this case is 0 + 262144 + 0 + 2952790016 totaling 2953052160.
    return temp;

    the value returned is the sum of all operations.

    }

    thanks for your help.

    2. Relevant equations

    This is a real world scenario function, what is it doing?

    3. The attempt at a solution

    In part (a), I have put in my findings at attempting the solution.
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Jun 23, 2008 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You will see what it is doing much easier if you express the input and output in hexadecimal rather than decimal format.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Bit shifting and bitwise operators
  1. Shift LR in 4 bit ALU? (Replies: 0)

Loading...