Timeline for How to deal with signed int overflows
Current License: CC BY-SA 3.0
5 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 15, 2018 at 1:47 | comment | added | user57037 | Glad my comments helped! A lot of us have been doing this stuff for a long time so it is now easy. But we have all had to figure it out along the way, usually with help from people who have done it before. It's never easy at first. You have to figure it out. | |
Apr 14, 2018 at 23:16 | comment | added | Shibalicious | You are completely correct, byte 2 is the lower byte, byte 3 is the higher one (it's a little-endian system). Yours and all the other comments have cleared the things a lot to me, it's much simpler than I thought it was gonna be. To be honest I have no idea where I got the idea of number overflows being stored in the byte nearby. It kinda makes sense and gives same results but the concept is totally different, overflows are performed on the entire value, rather than lower byte. Anyway, I know what I am doing now, thank you for your patience! | |
Apr 14, 2018 at 19:01 | comment | added | user57037 | @jonk, Yeah. I believe it is implementation defined. Might be undefined. But it works most of the time on two's complement machines. Doing it in such a way that it is fully portable is a pain in the ass. Could convert to unit32_t, then do some tests on the unsigned number and convert it into the correct int16_t without any implementation defined steps. But it is a pain. | |
Apr 14, 2018 at 18:57 | comment | added | jonk | For same-sized storage, C guarantees conversion of signed int to unsigned int. (Which doesn't sound like the direction the OP wants.) C does NOT guarantee conversion of unsigned int to signed int -- it only guarantees it where the unsigned int value has representation in the signed int (positive and fits.) Larger values can be turned into random garbage so far as the standard allows. Of course, no C compilers does that so far as I'm aware. But according to the spec, they could if they wanted to. Just as you say, I think. | |
Apr 14, 2018 at 18:55 | history | answered | user57037 | CC BY-SA 3.0 |