Bit Shift opertor

Discussion in 'C' started by naan, Sep 4, 2006.

  1. naan

    naan New Member

    Joined:
    Sep 4, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Can someone pls tell me how is the operation different in the following two code snippets?


    Code:
    main()
    {
    int temp=20742;
    
    short temp1;
    temp1 = temp << 8;
    printf("The vaue is %d\n",temp1>>8);}
    result:6

    Code:
    main()
    {
    int temp=20742;
    
    short temp1;
    temp1 =(temp << 8)>>8;
    printf("The vaue is %d\n",temp1);}
    result: 20742

    If I want to do the operation that I have done in the first code snippet in a single line without using any temp variables, how do I do it?

    Thanks
     
    Last edited by a moderator: Sep 4, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    temp1 =(temp << 8)>>8; when you do this the operation is done and the shift bits are not shreded and the operation is again performed on the same bits where as when you do in 2 lines the bits shifted is deleted and so the result is 6.
     

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