A short int always takes 2 bytes of memory, on every compiler. This note covers how it stores signed and unsigned data, and how its range is derived from 16 bits. It continues from Char Data Type, where the same range rules were derived for a 1 byte type.

Short Int Data Type

A variable of type short int is used to store 2 bytes (16 bits) of signed data. A variable of type unsigned short int is used to store 2 bytes of unsigned data.

You can also just write short or unsigned short. The int is always assumed:

short a;            // same as short int
unsigned short b;   // same as unsigned short int

A short always consumes 2 bytes of memory, irrespective of the compiler. You can check this yourself with sizeof(short) on any compiler and it will always be 2 bytes, using the operator explained in sizeof Operator.

2 Byte Signed Data Representation

A short int holds 16 bits, and one of them is the sign bit:

+-----------+--------------------------------------------------+
|  Bit 15   |               Bits 14 ... 0 (15 bits)            |
|  (MSB)    |                   Magnitude                     |
+-----------+--------------------------------------------------+
  sign bit
   0 → data is positive
   1 → data is negative
  • If the sign bit is 0, the data is positive, and the remaining 15 bits store the magnitude as plain binary.
  • If the sign bit is 1, the data is negative, and the remaining 15 bits store the magnitude in 2’s complement form.

Storing -25 in Two Bytes

Step 1: split the number. Sign = negative, magnitude = .

Step 2: write the magnitude in binary using all 16 bits:

Step 3: the data is negative, so the magnitude goes in 2’s complement form. First take the one’s complement (flip every bit), then add 1:

  +25:   0000 0000 0001 1001
  flip:  1111 1111 1110 0110        ← 1's complement
        +                1
        -----------------
  2's:  1111 1111 1110 0111        ← magnitude of 25 in 2's complement
         ↑
         sign bit = 1 → negative ✓

Step 4: convert to hex by splitting into nibbles (4-bit groups):

  1111 1111 1110 0111
   F    F    E    7          →  0xFFE7

Reading it back

The MSB is 1, so the value is . ✓

Storing +25 in Two Bytes

For a positive number, the sign bit is 0 and the magnitude is stored as plain binary. No 2’s complement is taken:

  0 000 0000 0001 1001     →  0000 0000 0001 1001  =  0x0019
  ↑
  positive

Deriving the Range

Signed (short int): the sign bit is fixed, and the remaining 15 bits hold the magnitude:

Unsigned (unsigned short int): there is no sign bit, so all 16 bits store the magnitude as plain binary:

  0000 0000 0000 0000  →      0   (smallest, all zeros)
  1111 1111 1111 1111  →  65,535  (largest, all ones)

The largest value is nothing but 0xFFFF in hex. This is the same general formula from Char Data Type > The General Formula, with bits. In the next lecture we will understand int and unsigned int.

Previous: Char Data Type