Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 10.5, shift left operator example
#2
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
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)