View Single Post
Posts: 362 | Thanked: 426 times | Joined on Nov 2010 @ Italy, Lombardia
#121
Originally Posted by pali View Post
No, It is same because first is multiply and then is byteshift. Try this:

Code:
printf("%#x %#x\n", 4*114 >> 2, 114);
printf("%#x %#x\n", 4*46 >> 2, 46);
output is:

Code:
0x72 0x72
0x2e 0x2e
Sorry but you are wrong.

My example was for a BYTE datatype, if you use an INT it is normal that it works as you expected because my numbers are too little to generate an overflow during left shift.

But please try with correct numbers
Code:
printf("%#x %#x\n", 4*1073741824>> 2, 1073741824);
printf("%#x %#x\n", 4*3075541994>> 2, 3075541994);
And the above code yields to these results:
Code:
0x0 0x40000000
0x37510bea 0xb7510bea
Numbers are very different now.

I reapet that C code like n<<VAR>>n (or (2^n)*VAR>>n) is usually used to clear the n most significative bits of VAR (if VAR is unsigned)

Last edited by Fabry; 2011-10-09 at 20:24. Reason: better english