Skip to main content
Visitor II
October 10, 2013
Question

<< shift easy question

  • October 10, 2013
  • 3 replies
  • 997 views
Posted on October 10, 2013 at 08:52

Hello, I have a simple question:

uint32_t temp;

temp |=  ((uint32_t)1 << 16);

without retyping it does not work (although if I add the expression to Watch, it shows expected 1 on 16th bit of temp). Why? I use Cosmic compiler and STM8S207...

Thank You

#shift
    This topic has been closed for replies.

    3 replies

    Visitor II
    October 11, 2013
    Posted on October 11, 2013 at 08:55

    Hello,

    I don't really understand what your problem is, but try to declare your variable as volatile, just to make sure that the compiler does not optimize away anything that you might not expect.

    Regards.

    pok-onAuthor
    Visitor II
    October 12, 2013
    Posted on October 12, 2013 at 14:17

    Hello luca,

    (1 << 16) does not set 16th bit

    ((uint32_t)(1 << 16) set 16th bit

    That is my problem. I would like to know the reason why. I have observed it with numbers >= 15.

    Visitor II
    October 21, 2013
    Posted on October 21, 2013 at 18:35

    that's because if you write 1 << 16 without any type cast, the calculation is done using the int type: on the stm8 that's 16 bits and it overflows.

    Regards,

    Luca