Help please

Discussion in 'C' started by adam_b19, Mar 29, 2007.

  1. adam_b19

    adam_b19 New Member

    Joined:
    Mar 29, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi can someone please help me,

    I have got this code below and I do not know what the "<<" and the ">>" operators functionalities are. The code is basically;

    Code:
    unsigned long nn, m, n;
    
    nn = 512;
    
    n=nn << 1;         //these are the 3 lines of code that I do not understand
    m=n >> 1;
    m >>= 1;
    Any help would be appreciated

    Adam
     
    Last edited by a moderator: Mar 30, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Please read about the proper way to post code. The operators you show are binary shift operators. In the first line the binary value, 512, is being shifted left one bit. This results in a value of 1024. In the second line, the value of n (now 1024) is being shifted right one bit. This results in the original value, 512. The third line is merely shorthand for "m = m >> 1;", which results in the value 256.

    In a left shift, the rightmost bit becomes a 0. In a right shift, the leftmost bit becomes a zero for unsigned variables, or a duplicate of its previous (before the shift) value for signed variables.
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    These are Bit wise shifting operators that shift bits of the variable provided on left hand side of << / >> , number of times provided on right hand side of operators. by discarding the carry bits......
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Thanks for clearing that up.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice