0

Can anyone explain the logic to me that how do I have been getting 32 as an output?

{ int a=4, b=2; a=b<<(a+b)>>2; printf("%d",a); }

10th Jan 2020, 9:35 AM
Aayush Srivastava
Aayush Srivastava - avatar
2 Respostas
+ 12
Basically the shift operators are used to shift the binary bits of number towards a direction. The binary scale is this: 1 2 4 8 16 32 64 128 256 ... (in reverse) Base 2. So if you do a b<<(a+b), [2<<6] you get the number from the scale that is at positive 6 distance from 2, that is 128 and then you shift that right by 2 which brings it to 32. The bitwise operators have high precedence and get evaluated in this order: a=((b<<(a+b))>>2) Try to always group them manually.
10th Jan 2020, 9:44 AM
Valen.H. ~
Valen.H. ~ - avatar
0
Valen.H. ~ Thank You 🙏
10th Jan 2020, 10:33 AM
Aayush Srivastava
Aayush Srivastava - avatar