Can any one tell me how the range of the data types are calculated? For example, in MySQL or PostgreSQL where we have smallint, integer and bigint, the range of smallint is from -32768 or +32767. How are these ranges calculated?

有帮助吗?

解决方案

Your data type is int which is represented as 2-bytes type. 2 bytes means 2 x 8 = 16 bits in it's binary representation. Also, your sample data type is signed - and that means highest bit will be used as sign (0 for positives, 1 for negatives).

From math it's known that with binary values you can hold 2^n unique values, i.e. for your 16-1 bits that will be 15 free bits. So that will be -(2^n) .. 2^n-1 range, where n=15. When you'll calculate -(2^15) and 2^15 - 1 you'll get your -32768 and 32767. That's it.

其他提示

Look to this pages in documentation

http://www.postgresql.org/docs/9.2/static/datatype-numeric.html

Numbers are based from data binary width 0111111111111111 ~ 32767, 1000000000000000 ~ -32768

you can find lot of documentation: search "complement format arithmetic numbers"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top