给定N位,可以用二进制表示多少个整数? [英] Given N bits, how many integers can be represented in binary?

查看:54
本文介绍了给定N位,可以用二进制表示多少个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有14位.您如何确定从这14位中可以用二进制表示多少个整数?

Suppose you have 14 bits. How do you determine how many integers can be represented in binary from those 14 bits?

仅仅是2 ^ n吗?所以2 ^ 14 = 16384?

Is it simply just 2^n? So 2^14 = 16384?

请注意问题的这一部分:二进制中可以表示多少个整数...".那就是我困惑的地方,否则这个问题看起来很简单.如果问题只是问从14位可以表示多少个不同的值或数字,那么肯定会是2 ^ n.

Please note this part of the question: "how many INTEGERS can be represented in BINARY...". That's where my confusion lies in what otherwise seems like a fairly straightforward question. If the question was just asking how many different values or numbers can be represented from 14 bits, than yes, I'm certain it's just 2^n.

推荐答案

答案取决于您需要带符号整数还是无符号整数.

The answer depends on whether you need signed or unsigned integers.

如果需要无符号整数,则可以使用 2 ^ n 表示从0到2 ^ n的整数.例如n = 2;2 ^ 2 = 4可以表示0到4之间的整数(不包括0到3).因此,使用n位,您可以表示最大无符号整数 2 ^ n-1 ,但总数为 2 ^ n 包括0的整数.

If you need unsigned integers then using 2^n you can represent integers from 0 to 2^n exclusive. e.g. n=2; 2^2=4 you can represent the integers from 0 to 4 exclusive (0 to 3 inclusive). Therefore with n bits, you can represent a maximum unsigned integer value of 2^n - 1, but a total count of 2^n different integers including 0.

如果需要带符号的整数,则一半的值是负数,一半的值是正数,并且1位用于指示整数是正数还是负数.然后,您可以使用 2 ^ n/2 进行计算.例如n = 2;2 ^ 2/2 = 2您可以表示-2到2的整数(不包括-2到+1).0被认为是正值,因此您将获得2个负值(-2,-1)和2个正值(0和+1).因此,使用n位,您可以表示(-)2 ^ n/2 (+)2 ^ n/n-1 ,但您仍然拥有与无符号整数相同的总数 2 ^ n 个不同的整数.

If you need signed integers, then half of the values are negative and half of the values are positive and 1 bit is used to indicate whether the integer is positive or negative. You then calculate using using 2^n/2. e.g. n=2; 2^2/2=2 you can represent the integers from -2 to 2 exclusive (-2 to +1 inclusive). 0 is considered postive, so you get 2 negative values (-2, -1) and 2 positive values (0 and +1). Therefore with n bits, you can represent signed integers values between (-) 2^n/2 and (+) 2^n/n - 1, but you still have a total count of 2^n different integers as you did with the unsigned integers.

这篇关于给定N位,可以用二进制表示多少个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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