19-02-2009, 01:03 PM
I suppose they are hinting between the lines that the 16 bit "mode" variable could contain values in the upper byte.
These are the declarations from the example:
uint8_t port = 0x5aU;
uint16_t mode;
First you take 0x5A left shift 4. The integer promotions will make "port" int, with the contents 0x5A0. Since the shift is performed on a 16 bit integer (or larger), nothing is shifted out.
Then you do bitwise AND with "mode". If "mode" contains anything in the higher byte, there is a potential for garbage values. Lets say that "mode" has the value 0xFFFF. Then all values in the upper byte are preserved, and the result will be 0x5A0 >> 6, giving 0x16 rather than as perhaps expected, 0xA0 >> 6 giving 0x02.
With an explicit cast (uint8_t)(port
These are the declarations from the example:
uint8_t port = 0x5aU;
uint16_t mode;
First you take 0x5A left shift 4. The integer promotions will make "port" int, with the contents 0x5A0. Since the shift is performed on a 16 bit integer (or larger), nothing is shifted out.
Then you do bitwise AND with "mode". If "mode" contains anything in the higher byte, there is a potential for garbage values. Lets say that "mode" has the value 0xFFFF. Then all values in the upper byte are preserved, and the result will be 0x5A0 >> 6, giving 0x16 rather than as perhaps expected, 0xA0 >> 6 giving 0x02.
With an explicit cast (uint8_t)(port
<t></t>