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

24.4k Views Asked by At

What is the difference between a word short and ushort in C#? They are both 16 bits!

2

There are 2 best solutions below

6
On

C# does not have a word type. If you mean short or Int16, the difference is that ushort is unsigned.

short can be any value from -32768 to 32767, whereas ushort can be from 0 to 65535. 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. So if your CPU (and operating system) are 32-bit, then a word is an Int32 or UInt32 (C#: int/uint). If you're on a 64-bit CPU/OS, a word is actually an 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.

0
On

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. In C# the data types has a fixed size and does not depend on the processor architecture.

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. The WORD type is also used in the Windows API with the same meaning.

So, the WORD data type corresponds to the C# type ushort.