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

Bit Shift opertor

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

  1. naan

    naan New Member

    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

    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