C# 数字数据类型命名

发布于 2024-09-17 08:17:54 字数 237 浏览 4 评论 0原文

查看 C# 数字数据类型,我注意到大多数类型都有有符号和无符号版本。我注意到,虽然“默认”整数、short 和 long 是有符号的,并且它们的无符号对应项是 uint、ushort 和 ulong; “默认”字节是无符号的,并且在 sbyte 中有一个有符号的对应字节。

只是出于好奇,为什么字节与其他的如此不同?这背后是否有特定的原因,或者“事情就是这样”?

希望这个问题不会因为我的措辞和过度使用引号而变得太混乱。呵呵..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疧_╮線 2024-09-24 08:17:54

我想说字节不被视为数字类型,而是定义了 8 位大小的结构。除了没有符号字节概念外,它还是无符号的。另一方面,数字首先被认为是有符号的,因此声明它们是无符号的,这是不常见的,需要前缀

[编辑]
忘记了有一个有符号字节(sbyte)。我认为这是相当历史和实际应用。 Int 比 UInt 更常见,而 byte 比 sbyte 更常见。

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.

厌倦 2024-09-24 08:17:54

从历史上看,术语“字节”、“半字节”和“位”表示存储单元、助记符或代码……而不是数值。拥有负兆字节的内存或添加 ASCII 代码 1 和 2 并期望代码 3 有点愚蠢。在许多方面,不存在有符号的“字节”这样的东西。有时“事物”和“值”之间的界限非常模糊......就像大多数将字节视为事物和值的语言一样。

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.

坠似风落 2024-09-24 08:17:54

更重要的是条款的某种程度的腐败。字节本质上并不是任何形式的数字,它只是一个存储单元。

然而,字节、字符和 8 位有符号/无符号整数的名称在它们可能不应该互换的地方可以互换使用:

  • 字节表示 8 位数据,说
    与数据格式无关。
  • 字符表示一些数据
    存储单个的表示
    文本字符。
  • “UINT8”/“INT8”表示 8 位
    数据,有符号或无符号格式,
    存储整数值。

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.
感受沵的脚步 2024-09-24 08:17:54

这实际上只是取决于直觉与一致性。如果 .NET Framework 使用 System.UInt8 和 System.Int8 来与其他整数类型保持一致,它可能会更清晰。但是,是的,这看起来确实有点武断。

就其价值而言,MSIL(所有 .NET 语言都编译为无论如何)更加一致,因为 sbyte 称为 int8byte称为 unsigned int8short 称为 int16 等。

但是术语“字节”通常不用于描述数字类型,而是用于描述数字类型。 8 位的集合,例如在处理文件、序列化、套接字等时。例如,如果 Stream.Read 使用 System.Int8[] 数组,那么这将是一个看起来非常不寻常的 API。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文