简体   繁体   English

C#中的short和ushort有什么区别?

[英]What is the difference between a short and ushort in C#?

What is the difference between a word short and ushort in C#? 是什么 之间的区别shortushort在C#中? They are both 16 bits! 它们都是16位!

C# does not have a word type. C#没有word类型。 If you mean short or Int16 , the difference is that ushort is unsigned. 如果你的意思是shortInt16 ,区别在于ushort是无符号的。

short can be any value from -32768 to 32767 , whereas ushort can be from 0 to 65535 . short可以是-3276832767任何值,而ushort可以是065535 They have the same total range and use the same number of bits but are interpreted in different ways, and have different maximums/minimums. 它们具有相同的总范围并使用相同的位数但以不同的方式解释,并且具有不同的最大值/最小值。

Clarification: A word is a general computer science term that is typically used to refer to the largest single group of bits that can be handled by the CPU in a single operation. 澄清:一个是一般的计算机科学术语,通常用于指代CPU可以在单个操作中处理的最大单个位组。 So if your CPU (and operating system) are 32-bit, then a word is an Int32 or UInt32 (C#: int / uint ). 因此,如果您的CPU(和操作系统)是32位,那么一个单词是Int32UInt32 (C#: int / uint )。 If you're on a 64-bit CPU/OS, a word is actually an Int64/UInt64 (C#: long / ulong ). 如果您使用的是64位CPU / OS,则单词实际上是Int64/UInt64 (C#: long / ulong )。 The term "word" usually refers only to the bit size of a variable as opposed to how it is actually interpreted in a program. 术语“字”通常仅指变量的位大小,而不是它在程序中的实际解释方式。

A (machine) word is the native size of the processor registers. (机器)字是处理器寄存器的原始大小。 It's generally what C has used as size for the int data type. 它通常是C用作int数据类型的大小。 In C# the data types has a fixed size and does not depend on the processor architecture. 在C#中,数据类型具有固定大小,并且不依赖于处理器体系结构。

In Intel assembly language the WORD data type has come to mean 16 bits, a DWORD (double word) is 32 bits and a QWORD (quad word) is 64 bits. 在Intel汇编语言中, WORD数据类型是16位, DWORD (双字)是32位, QWORD (四字)是64位。 The WORD type is also used in the Windows API with the same meaning. WORD类型也在Windows API中使用,具有相同的含义。

So, the WORD data type corresponds to the C# type ushort . 因此, WORD数据类型对应于C#类型的ushort

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

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