Hi,
what exactly happens when I try to store an integer in a byte-variable. Does it rollover or does it create an error?
Kevin
Hi,
what exactly happens when I try to store an integer in a byte-variable. Does it rollover or does it create an error?
Kevin
Firstly the compiler should throw an error.
If you force the issue you will clobber a byte on one side of the variable.
Is there a reason to do this?
Rob
It truncates, no error.
So, 1234 (0x4D2) will be stored as 210 (0xd2).
Firstly the compiler should throw an error.
The compiler will issue an "implicit narrowing cast" warning, but because of the Arduino "wrapper", you will never see it.
It truncates, no error.
So, 1234 (0x4D2) will be stored as 210 (0xd2).
thanks. As it triggers a service-interval it would not be a problem if it is (in sone few cases) to short...
Well cut of me legs and call me shorty, thanks AWOL, I learn something new every day, I was sure it would clobber a byte.
I just tried this
byte x;
int y = 1234;
x = y;
with -Wall, not even a warning. However
byte x;
int y = 1234;
x = 1234;
Gives "large integer implicitly truncated to unsigned type"
Rob
As it triggers a service-interval it would not be a problem if it is (in sone few cases) to short...
Can you explain what it is you want to do?
It may be more appropriate to store the most-significant byte if space is limited.
You can do this simply by dividing the "int" value by 256.
I decided to set it to a maximum of 255.
I found a very neat way of doing it. Think I should use those math-functions more often..
byte btVar = min(calculate, 255);
The compiler assumes that if you ask for
byte x;
int y = 1024;
x = y;
you really mean
x = y & 0xff; // get the lower 8 bits
However, if you attempt to initialize a byte to a value that is guaranteed to be out-of-range, the compiler will generate a warning.
KevinT:
I decided to set it to a maximum of 255.I found a very neat way of doing it. Think I should use those math-functions more often..
byte btVar = min(calculate, 255);
That would just make sure any out-of-range value would be maxed out at 255 - if that is the desired behavior, go ahead.