The result of the module is negative
Take a look at this line of code:
buf[i] = i%256;
i % 256
Evaluated here as a value of the type int
. It buf
is however an array char
s, so when a value is assigned to an array, it is truncated to char
. If a modulus result is outside the range of positive values โโthat can be stored in char
, it can wrap around and be stored instead of a negative number.
In other words, this does not mean that the module has produced a negative value as much as you have stored the result in a type that cannot hold it. Try changing the array to an array int
or an array unsigned char
and see if that fixes something.
Hope this helps!
source to share