1.why the max value form from integer 32 bit is (2^32-1)i think it should be 2^32
2.why the range of signed short int is -32768to32767
why 32767 is one lesser side than negative value
Based on Data types in c++
Hi @bansalpriyal45, the answer to both of your questions is that:
-
the counting starts from 0 AND NOT 1. Thus if counting of numbers were to start from 1 then the largest number that will be stored in in an unsigned int will be 2^32 i.e. 4294967296 but since we have to use 0 also thus max value stored is 2^32 - 1 .
-
Now when we deal with negative numbers, then the first bit of reserved memory is used to determine wether no. is +ve or -ve. 1 is used for -ve and 0 for +ve. Size of short signed int is 2 bytes i.e. 16 bits.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ = 16 bits = 2 bytes
this is either 1 or 0
so then we are left with 15 bits. max value that will be stored is 2^15 = 32768
so we can store 32768 negative numbers and 32768 +ve numbers.
When we store -ve numbers they start from -1 and go on to -32768 hence total 32768 numbers stored.
When we store +ve numbers we start count from 0 instead of 1 hence the max number that will be stores will be 2^15 - 1 i.e. 32767.
Hence the numbers stored are:
-32768, -32767 ,… -2, -1, 0, 1, 2, … 32766, 32767
[ ___ 32768 -ve nos.___ ] , [ __ 32768 +ve nos.__ ]
Hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.