uint8 vs int8 causes different outcomes

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

Hi,

   

The following evaluates true if the variables are int8, but not true if uint8 (which is correct).

   

            // Turn relays on if over set level
            if(relay[0] >= dataLocal.level + dataLocal.hysteresis)
            {
                FET_1_Write(1);
            }

   

The values are:  relay[0] = 0, dataLocal.level = 128, dataLocal.hysteresis = 10.

   

What am I missing here?  it seems like it is defying computer logic and calling 0 greater than 138...

   

Thank you,
Tom

0 Likes
1 Solution
Anonymous
Not applicable

Hi Tom

   
for uint8 Types  128u + 10u = 138u  for int Types  128 + 10 = -118 !   Reiner

View solution in original post

0 Likes
4 Replies
Anonymous
Not applicable

Hi Tom

   
for uint8 Types  128u + 10u = 138u  for int Types  128 + 10 = -118 !   Reiner
0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

Hi,

   

Is this because it is treating the 128 as a hex number?

   

Pardon my ignorance.

   

Regards,
Tom

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Partly.

   

The values an uint8 can represent are from 0 ro 255. From 255 it would overflow to 0

   

The values an int8 can represent are from -128 to 127, From 127 it would overflow to -128

   

 

   

Bob

ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

That is where I went wrong, thank you for the explanation, gentlemen.

   

Regards,

   

Tom

0 Likes