cancel
Showing results for 
Search instead for 
Did you mean: 

<< shift easy question

pok-on
Associate II
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
3 REPLIES 3
luca239955_stm1_st
Senior II
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
Associate II
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
Senior II
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