为什么无符号n位整数的最大值为2 ^ n-1而不是2 ^ n? [英] Why is the maximum value of an unsigned n-bit integer 2^n-1 and not 2^n?

查看:851
本文介绍了为什么无符号n位整数的最大值为2 ^ n-1而不是2 ^ n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

n -bit整数的最大值为2 n -1。为什么我们有减1?为什么不是最大值只有2 n

The maximum value of an n-bit integer is 2n-1. Why do we have the "minus 1"? Why isn't the maximum just 2n?

推荐答案

- 1 是因为整数从0开始,但我们的计数从1开始。

The -1 is because integers start at 0, but our counting starts at 1.

所以, 2 ^ 32-1 是32位无符号整数(32位二进制数字)的最大值 2 ^ 32 可能值的数量

So, 2^32-1 is the maximum value for a 32-bit unsigned integer (32 binary digits). 2^32 is the number of possible values.

为简化原因,请查看十进制。 10 ^ 2-1 是2位十进制数(99)的最大值。因为我们直观的人类计数从1开始,但整数从0开始, 10 ^ 2 是值的数量(100)。

To simplify why, look at decimal. 10^2-1 is the maximum value of a 2-digit decimal number (99). Because our intuitive human counting starts at 1, but integers are 0-based, 10^2 is the number of values (100).

这篇关于为什么无符号n位整数的最大值为2 ^ n-1而不是2 ^ n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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