## #1,112 – How Integers Are Stored in .NET

June 6, 2014 1 Comment

Integer values in .NET are stored in memory as described below. The examples below use a 2-byte **short** data type as an example.

A value of zero is stored by setting every bit in the storage location to zero.

Positive values are stored directly as their corresponding binary number.

The maximum possible integer value has all bits set to 1 except for the leftmost. So Max = 2^(n-1) – 1, where n is the total number of bits.

Negative integer values are stored using a *two’s complement*** **representation, calculated by starting with the bit pattern for the corresponding positive number, negating all of the bits, and then adding 1 to the result.

The minimum possible integer value (largest negative value) has only the leftmost bit set to 1. So Min = -(2^(n-1)), where n is the total number of bits.