3 ms·
>> uint8_t gets promoted to a signed int when shifting, which in turn causes undefined behavior for specific input. >> One way of fixing this is casting to the
by zimmerfrei 4y ago
>> uint8_t gets promoted to a signed int when shifting, which in turn causes undefined behavior for specific input.
>> One way of fixing this is casting to the desired type before the shift, thus avoiding surprising conversions.
Agree. For clarity, the "specific input" in the example would be a bytes[0] value larger than 127.
This behavior is also explained here:
https://wiki.sei.cmu.edu/confluence/display/c/INT34-C.+Do+not+shift+an+expression+by+a+negative+number+of+bits+or+by+greater+than+or+equal+to+the+number+of+bits+that+exist+in+the+operand https://wiki.sei.cmu.edu/confluence/display/c/INT34-C.+Do+no...