Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream.ReadByte: Byte's are never negative numbers?

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?

like image 740
richard Avatar asked Jun 15 '11 06:06

richard


People also ask

Can byte store negative numbers?

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 ).

What is a negative byte?

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.


2 Answers

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.)

like image 68
Guffa Avatar answered Sep 24 '22 10:09

Guffa


Because Bytes are not negative, they're numbers in the value 0-255.

like image 30
pistacchio Avatar answered Sep 21 '22 10:09

pistacchio