Skip to main content
hgprojekt
Associate II
May 16, 2017
Question

Result of logic operation on 8-bits data need to casts

  • May 16, 2017
  • 2 replies
  • 1946 views
Posted on May 16, 2017 at 23:42

Hello,

In STVD (+Cosmic) C compiler settings I choose *Display Errors & Warnings*.

I wrote simple code:

uint8_t a=0x01;

uint8_t b=0x10;

void test (uint8_t licz) {

}

void main (void) {

    test (a & 0x01);

    test (a | b);

    test (a + b);

}

After compilation there are warnings:

#warning cpstm8 main.c:122(9) truncating assignment

#warning cpstm8 main.c:123(9) truncating assignment

#warning cpstm8 main.c:124(10) truncating assignment

To avoid warnings I need to use casts, for example:

test ((uint8_t)(a & 0x01));

The result of (a + b) operation do not exceed max value 8-bit long data.

Why are there warnings?

All data are declared as 8-bits long.

If the data will be declared as integer 16-bits long there’s no need to use casts.

    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    May 16, 2017
    Posted on May 17, 2017 at 00:27

    >>The result of (a + b) operation do not exceed max value 8-bit long data.

    Yeah in your specific case, it is not optimizing/warning about that case or with your constants, it is complaining about the general case where the values change and 240 + 230 doesn't fit.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    hgprojekt
    hgprojektAuthor
    Associate II
    May 17, 2017
    Posted on May 17, 2017 at 02:53

    I understand that, but why are there warnings in case of

    test (a | b);

    and

    test (a & 0x01);

    ?

    These expressions will never exceed max value 8-bit long data.

    Moreover, if there will be all 16-bit data, and value for example

    uint16_t a=0x0001;

    uint16_t b=0x1000;

    The same logic and arithmetic operations don’t generate any warnings.

    luca239955_stm1_st
    Associate
    May 18, 2017
    Posted on May 18, 2017 at 09:37

    that's because the C language requires a lot of implicit conversions to int: if you write

        test ((unsigned char)(a & 0x01));

        test ((unsigned char)(a | b));

    the warnings will go away.

    If a and b are already 16 bits the implicit conversion to int (that is 16 bits on the stm8) does not change anything, hence no warning.

    Regards,

    Luca