views:

154

answers:

4

Looking at the C# numeric data types, I noticed that most of the types have a signed and unsigned version. I noticed that whereas the "default" integer, short and long are signed, and have their unsigned counterpart as uint, ushort and ulong; the "default" byte is instead unsigned - and have a signed counterpart in sbyte.

Just out of curiosity, why is byte so different from the rest? Was there a specific reason behind this or it is "just the way things are"?

Hope the question isn't too confusing due to my phrasing and excessive use of quotes. Heh..

+4  A: 

I would say a byte is not considered a numeric type but defines a structure with 8 bits in size. Besides there is no signed byte notion, it is unsigned. Numbers on the otherhand are firstly considered to be signed, so stating they are unsigned which is less common warrants the prefix

[EDIT] Forgot there is a signed byte (sbyte). I suppose it is rather historical and practical application. Ints are more common than UInts and byte is more common than sbyte.

aqwert
Interesting... I've always seen it being lumped together with the numeric types in most books and sites (e.g. Numeric, String, Others, etc.)
Darkwoof
It is definately a type, but have you ever used a byte in mathematics outside of computer science. You have with the other types ;)
aqwert
System.Byte is just as much a numeric type as the other integer types. It supports all the same operators, formatting specifiers, etc. In fact in IL it's called an unsigned int8.
Josh Einstein
Actually I see what you mean now and I agree... The term byte is not really numeric, although the .NET structure is indeed a fully supported numeric type.
Josh Einstein
Ahh... having the names decided on by how common they are makes sense. =)
Darkwoof
+1  A: 

It really just comes down to being intuitive versus being consistent. It probably would have been cleaner if the .NET Framework used System.UInt8 and System.Int8 for consistency with the other integer types. But yeah it does seem a bit arbitrary.

For what it's worth MSIL (which all .NET languages compile to anyhow) is more consistent in that a sbyte is called an int8 and a byte is called an unsigned int8, short is called int16, etc.

But the term byte is typically not used to describe a numeric type but rather a set of 8 bits such as when dealing with files, serialization, sockets, etc. For example if Stream.Read worked with a System.Int8[] array, that would be a very unusual looking API.

Josh Einstein
Do you mean `ubyte` in your first paragraph? `byte` and `sbyte` *are* what C# went with.
Dan Tao
I guess it didn't come out right but I meant that I like C# using byte and sbyte as it does but for consistency it feels like they should map to System.UInt8 and System.Int8 respectively.
Josh Einstein
+2  A: 

Historically the terms byte, nibble and bit indicate a unit of storage, a mnemonic or code...not a numeric value. Having negative mega-bytes of memory or adding ASCII codes 1 and 2 expecting code 3 is kinda silly. In many ways there is no such thing as a signed "byte". Sometimes the line between "thing" and "value" is very blurry....as with most languages that treat byte as a thing and a value.

Rusty
+1  A: 

It's more so a degree of corruption of the terms. A byte is not inherently numeric in any form, it's simply a unit of storage.

However, bytes, characters, and 8-bit signed/unsigned integers have had their names used interchangeably where they probably should not have:

  • Byte denotes 8 bits of data, says nothing about the format of the data.
  • Character denotes some data that stores a representation of a single text character.
  • "UINT8"/"INT8" denotes 8 bits of data, in signed or unsigned format, storing numeric integer values.
Sion Sheevok