Skip to main content
This topic has been closed for replies.
berendi
berendiBest answer
Principal
Jul 12, 2020 10:51 AM UTC

Yes, this is the effect of integer promotion. Short and char types are converted to signed int every time they are used in an expression. Unsigned shorts are extended with 0 bits (0x81d0 -> 0x000081d0), signed are extended with the value of bit 15 (0x81d0 -> 0xffff81d0).

5 replies

berendi
Principal
Jul 11, 2020 9:30 AM UTC

Short is a 16 bit signed integer type in the range from -0x8000 to 0x7fff, so 0x81d0 is out of its range. Therefore the condition is never true.

​

Read the warnings issued by the compiler, they are there for a reason.

​

​Review the integer types and promotion rules, otherwise you would get a few more surprises with types that are less than 32 bits wide.

olezhandr
olezhandrAuthor
Associate III
Jul 11, 2020 12:45 PM UTC

if I change variable to <C> from <symb> it works corretly but <C> and <symb> are unsigned short type as you can see from my screen

berendi
Principal
Jul 11, 2020 1:37 PM UTC

C is unsigned, symb is signed.

When neither signed nor unsigned is explicitly stated, then the default is signed. (Except for the char type, where it depends on the compiler configuration and command line options, so you can never be sure.)

olezhandr
olezhandrAuthor
Associate III
Jul 11, 2020 2:09 PM UTC

Thank you! I understand my mistake! I thought that sign store at 15 bit.. but it stored at 31

berendi
berendiBest answer
Principal
Jul 12, 2020 10:51 AM UTC

Yes, this is the effect of integer promotion. Short and char types are converted to signed int every time they are used in an expression. Unsigned shorts are extended with 0 bits (0x81d0 -> 0x000081d0), signed are extended with the value of bit 15 (0x81d0 -> 0xffff81d0).