This question is from danchisholm:
it's asked for the output of that program which is 31,0
Explanation:
The expression (-1 & 0x1f) is equal to (0xffffffff & 0x1f), and both are equal to the hex value 0x1f or decimal 31. The expression (8 << -1) is equivalent to (8 << 0xffffffff). If the left hand operand of a shift expression is of type int, then the right hand operand is implicitly masked with the value 0x1f. In other words, the expression (8 << -1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with the hexadecimal representation we have (8 << (0xffffffff & 0x1f)). By evaluating the right hand operand we have (8 << 31). When 8 is shifted 31 bits to the left, the result is zero since the only non-zero bit is lost as it is shifted beyond the most significant bit of the int data type.
My doubt:
What does it mean that when the left-hand operand is of type int the right-hand operand is implicity masked with 0x1f ? Is it because the right-hand operand is a negative number ? What would happend if the left-hand operand is of type long, then what ?
Please, somebody explain when this implicit mask is performed.
Thanks,
Francisco