From msdn:
FileSystem.ReadByte
The byte, cast to an Int32, or -1 if the end of the stream has been reached.
So -1 is basically a "magic value". Does this mean the bytes returned from streams are never negative? If not, why not?
In C#, a byte represents an unsigned 8-bit integer, and can therefore not hold a negative value (valid values range from 0 to 255 ).
Negative numbers are stored by decrementing from zero, thus -1 value is always 2(N)-1. The lowest negative number that can be represented is -2(N-1). For the byte case we have 8 bits, even though unsigned values for a byte range from zero up to 255, in Java it's regarded as signed, thus they range from -128 up to 127.
In C# the values of a byte
is a number between 0 and 255, so a byte is never negative.
The sbyte
data type is a signed byte, so it has a value between -128 and 127.
(In some other languages, for example Java, the byte
data type is signed.)
Because Bytes are not negative, they're numbers in the value 0-255.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With