Bit Shift opertor

naan's Avatar, Join Date: Sep 2006
Newbie 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 shabbir; 4Sep2006 at 16:58.. Reason: Code formating.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.