简体   繁体   English

为什么c#中的字节被命名为byte和sbyte,与其他整数类型不同?

[英]Why bytes in c# are named byte and sbyte unlike other integral types?

I was just flipping through the specification and found that byte is odd. 我只是翻阅规范,发现字节是奇数。 Others are short, ushort, int, uint, long, and ulong. 其他的很短,ushort,int,uint,long和ulong。 Why this naming of sbyte and byte instead of byte and ubyte? 为什么这个sbyte和byte的命名而不是byte和ubyte?

It's a matter of semantics. 这是一个语义问题。 When you think of a byte you usually (at least I do) think of an 8-bit value from 0-255. 当你想到一个字节时,你通常(至少我)会想到0到25​​5之间的8位值。 So that's what byte is. 这就是byte The less common interpretation of the binary data is a signed value ( sbyte ) of -128 to 127. 对二进制数据的不太常见的解释是-128到127的带符号值( sbyte )。

With integers, it's more intuitive to think in terms of signed values, so that's what the basic name style represents. 使用整数,从签名值的角度思考更直观,这就是基本名称样式所代表的含义。 The u prefix then allows access to the less common unsigned semantics. 然后, u前缀允许访问不太常见的无符号语义。

The reason a type "byte", without any other adjective, is often unsigned while a type "int", without any other adjective, is often signed, is that unsigned 8-bit values are often more practical (and thus widely used) than signed bytes, but signed integers of larger types are often more practical (and thus widely used) than unsigned integers of such types. 没有任何其他形容词的类型“byte”的原因通常是无符号的,而没有任何其他形容词的类型“int”经常是有符号的,因为无符号8位值通常比实际(并因此广泛使用)更实用(因此广泛使用)有符号字节,但较大类型的有符号整数通常比这类类型的无符号整数更实用(因而广泛使用)。

There is a common linguistic principle that, if a "thing" comes in two types, "usual" and "unusual", the term "thing" without an adjective means a "usual thing"; 有一个共同的语言原则,如果“事物”有两种类型,“通常”和“不寻常”,没有形容词的“事物”一词意味着“通常的事物”; the term "unusual thing" is used to refer to the unusual type. 术语“不寻常的东西”用于指不寻常的类型。 Following that principle, since unsigned 8-bit quantities are more widely used than signed ones, the term "byte" without modifiers refers to the unsigned flavor. 遵循该原则,由于无符号8位量的使用比带符号的更广泛,因此没有修饰符的术语“字节”指的是无符号的味道。 Conversely, since signed integers of larger sizes are more widely used than their unsigned equivalents, terms like "int" and "long" refer to the signed flavors. 相反,由于较大尺寸的有符号整数比其无符号等价物更广泛使用,因此诸如“int”和“long”之类的术语指的是带符号的风格。

As for the reason behind such usage patterns, if one is performing maths on numbers of a certain size, it generally won't matter--outside of comparisons--whether the numbers are signed or unsigned. 至于这种使用模式背后的原因,如果一个人对一定数量的数字进行数学运算,通常无关紧要 - 在比较之外 - 数字是有符号还是无符号。 There are times when it's convenient to regard them as signed (it's more natural, for example, to say think in terms of adding -1 to a number than adding 65535) but for the most part, declaring numbers to be signed doesn't require any extra work for the compiler except when one is either performing comparisons or extending the numbers to a larger size. 有时候将它们视为签名是很方便的(例如,考虑将数字加-1而不是添加65535就更自然了)但是在大多数情况下,声明要签名的数字并不需要编译器的任何额外工作,除非是执行比较或将数字扩展到更大的大小。 Indeed, if anything, signed integer math may be faster than unsigned integer math (since unsigned integer math is required to behave predictably in case of overflow, whereas unsigned math isn't). 实际上,如果有的话,有符号整数数学可能比无符号整数数学更快(因为无符号整数数学需要在溢出的情况下可预测地表现,而无符号数学不是)。

By contrast, since 8-bit operands must be extended to type 'int' before performing any math upon them, the compiler must generate different code to handle signed and unsigned operands; 相反,由于在对它们执行任何数学运算之前必须将8位操作数扩展为类型'int',因此编译器必须生成不同的代码来处理有符号和无符号操作数; in most cases, the signed operands will require more code than unsigned ones. 在大多数情况下,签名操作数将需要比无符号操作数更多的代码。 Thus, in cases where it wouldn't matter whether an 8-bit value was signed or unsigned, it often makes more sense to use unsigned values. 因此,在8位值是有符号还是无符号无关紧要的情况下,使用无符号值通常更有意义。 Further, numbers of larger types are often decomposed into a sequence of 8-bit values or reconstituted from such a sequence. 此外,较大类型的数量通常被分解为8位值的序列或者从这样的序列重构。 Such operations are easier with 8-bit unsigned types than with 8-bit signed types. 使用8位无符号类型比使用8位有符号类型更容易进行此类操作。 For these reasons, among others, unsigned 8-bit values are used much more commonly than signed 8-bit values. 由于这些原因,除了其他原因之外,无符号8位值比带符号的8位值更常用。

Note that in the C language, "char" is an odd case, since all characters within the C character set are required to translate as non-negative values (so machines which use an 8-bit char type with an EBCDIC character set are required to have "char" be unsigned), but an "int" is required to hold all values that a "char" can hold (so machines where both "char" and "int" are 16 bits are required to have "char" be signed). 请注意,在C语言中,“char”是一个奇怪的情况,因为C字符集中的所有字符都需要转换为非负值(因此需要使用带有EBCDIC字符集的8位字符类型的机器)让“char”成为无符号),但需要“int”来保存“char”可以容纳的所有值(因此“char”和“int”都是16位的机器需要“char”为签)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM