为什么负整数比正整数多一个? [英] Why is there one more negative int than positive int?

查看:15
本文介绍了为什么负整数比正整数多一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何 int 数据类型(不包括 tinyint)的上限总是比下限的绝对值小一.

The upper limit for any int data type (excluding tinyint), is always one less than the absolute value of the lower limit.

例如,int 的上限为 2,147,483,647,而 ABS(下限)= 2,147,483,648.

For example, the upper limit for an int is 2,147,483,647 and ABS(lower limit) = 2,147,483,648.

负整数总比正整数多一个有什么原因吗?

Is there a reason why there is always one more negative int than positive int?

已更改,因为问题与数据库没有直接关系

推荐答案

您提供的类型是有符号整数.让我们看一个字节(8 位)的例子.使用 1 个字节,您有 2^8 组合,可以存储 256 个可能的数字.

The types you provided are signed integers. Let's see one byte(8-bit) example. With 1 byte you have 2^8 combinations which gives you 256 possible numbers to store.

现在你想拥有相同数量的正数和负数(每组应该有 128 个).

Now you want to have the same number of positive and negative numbers (each group should have 128).

重点是0没有+0-0.只有一个0.

The point is 0 doesn't have +0 and -0. There is only one 0.

所以你最终得到范围 -128..-1..0..1..127.

So you end up with range -128..-1..0..1..127.

同样的逻辑适用于 16/32/64 位.

为什么范围是 -128 到 127?

这取决于你如何表示有符号整数:

这篇关于为什么负整数比正整数多一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆