Skip to main content
pok-on
Associate II
Oct 10, 2013 6:52 AM UTC
Question

<< shift easy question

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

    luca239955_stm1_st
    Associate
    Oct 11, 2013 6:55 AM UTC
    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-on
    pok-onAuthor
    Associate II
    Oct 12, 2013 12:17 PM UTC
    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.

    luca239955_stm1_st
    Associate
    Oct 21, 2013 4:35 PM UTC
    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