Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Bit Shift opertor (http://www.go4expert.com/forums/bit-shift-opertor-t1340/)

naan 4Sep2006 14:37

Bit Shift opertor
 
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

shabbir 4Sep2006 17:03

Re: Bit Shift opertor
 
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.


All times are GMT +5.5. The time now is 12:23.