www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why (v1<<8 + v2) different from (v1<<8 | v2)?

reply step8 <step7 163.com> writes:
     I run following test code:
       int v1 = 22;
       int v2 = 23;
       writeln( v1<<8 + v2 );		
       writeln( v1<<8 | v2 );

result is 0 and 5655
Why ( v1<<8 + v2 ) = 0 ?
May 25 2022
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 25 May 2022 at 12:42:04 UTC, step8 wrote:
     I run following test code:
       int v1 = 22;
       int v2 = 23;
       writeln( v1<<8 + v2 );		
       writeln( v1<<8 | v2 );

 result is 0 and 5655
 Why ( v1<<8 + v2 ) = 0 ?
`+` has a higher precedence than `<<`, so the first line is actually `v1 << (8 + v2)`.
May 25 2022
parent step8 <step7 163.com> writes:
Thanks:)
**writeln( (v1<<8) + v2 );**   is ok	
On Wednesday, 25 May 2022 at 12:51:07 UTC, Paul Backus wrote:
 On Wednesday, 25 May 2022 at 12:42:04 UTC, step8 wrote:
     I run following test code:
       int v1 = 22;
       int v2 = 23;
       writeln( v1<<8 + v2 );		
       writeln( v1<<8 | v2 );

 result is 0 and 5655
 Why ( v1<<8 + v2 ) = 0 ?
`+` has a higher precedence than `<<`, so the first line is actually `v1 << (8 + v2)`.
(
May 25 2022