Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

C# unsigned types: uint, ulong, ushort and sbyte

Reference for C# uint, ulong, ushort and sbyte: ranges, literal suffixes, wrap-around, mixing signed and unsigned values, and when to use them.

C# has four integer types that sit beside the everyday int and long. uint, ulong and ushort hold only zero and positive values, which doubles the positive range compared with their signed twins. sbyte is the odd one out: it is the signed 8-bit type, because plain byte is already unsigned.

Unsigned types are common in file formats, network protocols, hashing and bit manipulation, where a value is a pattern of bits rather than a quantity that can go negative. They are rarely the right choice for ordinary counters or sizes in application code, because subtracting past zero does not give a negative number.

C#
uint flags = 0xFFu;
ulong big = 18_000_000_000_000_000_000UL;
ushort port = 8080;
sbyte delta = -5;

Try it

C# Ranges and literal suffixes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Wrap-around and checked

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
sbyte  s = -128;          // System.SByte,  8 bits, signed
ushort u16 = 65535;       // System.UInt16, 16 bits
uint   u32 = 4_294_967_295u;   // System.UInt32, 32 bits
ulong  u64 = 1UL << 63;   // System.UInt64, 64 bits
Keyword .NET type Size Range
sbyte System.SByte 8 bits -128 to 127
ushort System.UInt16 16 bits 0 to 65,535
uint System.UInt32 32 bits 0 to 4,294,967,295
ulong System.UInt64 64 bits 0 to 18,446,744,073,709,551,615
Literal suffix Type
u or U uint, or ulong if the value does not fit
l or L long (prefer L; lowercase looks like the digit 1)
ul, UL, lu and so on ulong

There are no suffixes for sbyte and ushort. An integer literal that fits is converted implicitly, so ushort p = 8080; works.

Errors / undefined behavior. Arithmetic that leaves the range wraps around in the default unchecked context and throws OverflowException in a checked context or with the project setting CheckForOverflowUnderflow. Constant expressions that overflow are compile errors. Integer division by zero throws DivideByZeroException.

Usage notes

  • Subtraction is the trap. uint a = 3, b = 5; a - b is 4294967294, not -2. A loop like for (uint i = n - 1; i >= 0; i--) never ends, because i >= 0 is always true.
  • Mixing signs. uint combined with int is promoted to long, so the result is exact; ulong combined with a signed type is a compile error because no type holds both ranges. Cast explicitly.
  • Small types promote. Arithmetic on sbyte and ushort is done in int, so ushort a = 1, b = 2; var c = a + b; is an int. Assigning back needs a cast.
  • Not CLS-compliant. uint, ulong, ushort and sbyte are not part of the Common Language Specification, so public APIs meant for every .NET language usually avoid them. The BCL itself uses int for lengths and indexes.
  • Bit work. Prefer unsigned types for shifts and masks: >> on an unsigned value always fills with zeros, while on a signed one it keeps the sign bit. The >>> operator gives the same zero-fill for signed types.
  • Parsing and formatting. Each type has Parse, TryParse, ToString("X") for hex and the generic-math interfaces such as IBinaryInteger<T>.
  • Performance. On 64-bit hardware there is no speed difference between signed and unsigned arithmetic of the same width; choose by meaning, not by speed.