What are the maximum and minimum numbers that can be stored in C?

It’s hard for me to write data types to C. I am looking at the book C, and one of the problems is asking what can store the maximum and minimum number a short .

Using sizeof(short); , I see that a short flow takes 2 bytes. This means that it is 16 bits, which means two numbers, since it takes 8 bits to store the binary representation of the number. For example, 9 would be 00111001 , which fills one bit. So wouldn’t it be from 0 to 99 for unsigned and from -9 to 9 signed?

I know I'm wrong, but I don’t know why. It indicates here the maximum (-) 32,767 for signatories and 65,535 for unsigned.

short int, 2 bytes, 16 bits, -32,768 → +32,767 Range (16kb)

+7
source share
5 answers

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

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

If you have a number like this:

 01011101 

It:

 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 larger values ​​than 9 in one byte. In an 8-bit unsigned number, you can store values ​​from 00000000 to 11111111 , which is equal to 255 in decimal form.

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

In your statement, “it takes 8 bits to store the binary representation of a number” - this is how to say “it takes 8 digits to store the decimal representation of a number,” which is incorrect. For example, the number 12345678901234567890 has more than 8 digits. Similarly, you cannot put all numbers in 8 bits, but only 256 of them. So you get 2-byte ( short ), 4-byte ( int ) and 8-byte ( long long ) numbers. In truth, if you need an even larger range of numbers, you will need to use a library.

As long as we are talking about negative numbers, on a computer with an add-on, it's just an agreement to use the higher half of the range as negative values. This means that numbers with 1 on the left side are considered negative.

However, these digits are congruent modulo 256 (modulo 2^n if n bits) to their positive value, because the number does offer, for example, the number 11111111 is 255 if unsigned, and -1 if they match congruent modulo 256.

+11
source

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

+7
source

The link you are reading is correct. At least for ordinary C implementations, where short is 16 bits, which are actually not fixed in the standard.

16 bits can contain 2 ^ 16 possible bit patterns, which is 65,536 possibilities. Signed shorts: from -32768 to 32767, unsigned - from 0 to 65535.

+7
source

Others have made some pretty good decisions for you, but I don’t think they followed your thinking and explained where you were wrong. I'll try.

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

Up to this point, you are right (although short not guaranteed to be 2 bytes long, as int not guaranteed to be 4 - the only guaranteed size by the standard (if I remember correctly) char which should always be 1 byte wide).

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

From here you started to drift a little. 8 bits are not really required to store the number. Depending on the number, 16, 32 64, or even more bits may be required to store it. Splitting your 16 bits into 2 is wrong. If this is not a specification for the implementation of the CPU, we could have, for example, 2-bit numbers. In this case, these two bits can store values ​​such as:

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

To save 4, we need 3 bits. And therefore, the value "not suitable", which will lead to overflow. The same goes for the 16-bit number. For example, let's say that we have unsigned “255” in decimal format stored in 16 bits, the binary representation will be 0000000011111111 . When you add 1 to this number, it becomes 0000000100000000 (256 in decimal). Therefore, if you had only 8 bits, it would overflow and become 0, because the most significant bit would be discarded.

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

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

Hope this helps!

+3
source

The formula for finding the range of any unsigned binary number is:

 2 ^ (sizeof(type)*8) 
0
source

All Articles