Mohamad
Mohamad

Reputation: 35339

What are the max and min numbers a short type can store in C?

I'm having a hard time grasping data types in C. I'm going through a C book and one of the challenges asks what the maximum and minimum number a short can store.

Using sizeof(short); I can see that a short consumes 2 bytes. That means it's 16 bits, which means two numbers since it takes 8 bits to store the binary representation of a number. For example, 9 would be 00111001 which fills up one bit. So would it not be 0 to 99 for unsigned, and -9 to 9 signed?

I know I'm wrong, but I'm not sure why. It says here the maximum is (-)32,767 for signed, and 65,535 for unsigned.

short int, 2 Bytes, 16 Bits, -32,768 -> +32,767 Range (16kb)

Upvotes: 12

Views: 67133

Answers (5)

user405725
user405725

Reputation:

Others have posted pretty good solutions for you, but I don't think they have followed your thinking and explained where you were wrong. I will try.

I can see that a short consumes 2 bytes. That means it's 16 bits,

Up to this point you are correct (though short is not guaranteed to be 2 bytes long like int is not guaranteed to be 4 — the only guaranteed size by standard (if I remember correctly) is char which should always be 1 byte wide).

which means two numbers since it takes 8 bits to store the binary representation of a number.

From here you started to drift a bit. It doesn't really take 8 bits to store a number. Depending on a number, it may take 16, 32 64 or even more bits to store it. Dividing your 16 bits into 2 is wrong. If not a CPU implementation specifics, we could have had, for example, 2 bit numbers. In that case, those two bits could store values like:

00 - 0 in decimal
01 - 1 in decimal
10 - 2 in decimal
11 - 3 in decimal

To store 4, we need 3 bits. And so the value would "not fit" causing an overflow. Same applies to 16-bit number. For example, say we have unsigned "255" in decimal stored in 16-bits, the binary representation would be 0000000011111111. When you add 1 to that number, it becomes 0000000100000000 (256 in decimal). So if you had only 8 bits, it would overflow and become 0 because the most significant bit would have been discarded.

Now, the maximum unsigned number you can in 16 bits memory is — 1111111111111111, which is 65535 in decimal. In other words, for unsigned numbers - set all bits to 1 and that will yield you the maximum possible value.

For signed numbers, however, the most significant bit represents a sign — 0 for positive and 1 for negative. For negative, the maximum value is 1000000000000000, which is -32678 in base 10. The rules for signed binary representation are well described here.

Hope it helps!

Upvotes: 3

Shahbaz
Shahbaz

Reputation: 47493

Think in decimal for a second. If you have only 2 digits for a number, that means you can store from 00 to 99 in them. If you have 4 digits, that range becomes 0000 to 9999.

A binary number is similar to decimal, except the digits can be only 0 and 1, instead of 0, 1, 2, 3, ..., 9.

If you have a number like this:

01011101

This is:

0*128 + 1*64 + 0*32 + 1*16 + 1*8 + 1*4 + 0*2 + 1*1 = 93

So as you can see, you can store bigger values than 9 in one byte. In an unsigned 8-bit number, you can actually store values from 00000000 to 11111111, which is 255 in decimal.

In a 2-byte number, this range becomes from 00000000 00000000 to 11111111 11111111 which happens to be 65535.

Your statement "it takes 8 bits to store the binary representation of a number" is like saying "it takes 8 digits to store the decimal representation of a number", which is not correct. For example the number 12345678901234567890 has more than 8 digits. In the same way, you cannot fit all numbers in 8 bits, but only 256 of them. That's why you get 2-byte (short), 4-byte (int) and 8-byte (long long) numbers. In truth, if you need even higher range of numbers, you would need to use a library.

As long as negative numbers are concerned, in a 2's-complement computer, they are just a convention to use the higher half of the range as negative values. This means the numbers that have a 1 on the left side are considered negative.

Nevertheless, these numbers are congruent modulo 256 (modulo 2^n if n bits) to their positive value as the number really suggests. For example the number 11111111 is 255 if unsigned, and -1 if signed which are congruent modulo 256.

Upvotes: 19

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

This is defined in <limits.h>, and is SHRT_MIN & SHRT_MAX.

Upvotes: 9

Linuxios
Linuxios

Reputation: 35783

The formula to find the range of any unsigned binary represented number:

2 ^ (sizeof(type)*8)

Upvotes: 1

Keith Randall
Keith Randall

Reputation: 23265

The reference you read is correct. At least, for the usual C implementations where short is 16 bits - that's not actually fixed in the standard.

16 bits can hold 2^16 possible bit patterns, that's 65536 possibilities. Signed shorts are -32768 to 32767, unsigned shorts are 0 to 65535.

Upvotes: 11

Related Questions