java 如何计算原始数据类型的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16983341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate the range of primitive data types?
提问by Ankit
According to docs.oracle.com:-
根据 docs.oracle.com:-
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
byte:字节数据类型是一个 8 位有符号二进制补码整数。它的最小值为 -128,最大值为 127(含)。字节数据类型可用于在大型数组中节省内存,其中内存节省实际上很重要。它们也可以代替 int 使用它们的限制有助于澄清您的代码;变量范围有限的事实可以作为一种文档形式。
Byte - 8 bits
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
字节 - 8 位
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1)
128 64 32 16 8 4 2 1)
Adding all these numbers we get a total of 255. Then how's the range which is -128 to 127 calculated. Is it hard-coded somewhere or there's some more technicality to this range?
将所有这些数字相加,我们得到总共 255。那么 -128 到 127 的范围是如何计算的。它是在某处进行了硬编码还是在这个范围内有更多的技术性?
Any suggestions would be appreciated.
任何建议,将不胜感激。
回答by LuigiEdlCarno
It is a signed type, meaning, it still has a range of 255 (as you have correctly calculated), but it starts at -128. So half the range is below zero, 1 possible number is = (zero) and the remaining 127 are above 0.
它是有符号类型,这意味着它的范围仍然是 255(正如您正确计算的那样),但它从 -128 开始。因此,范围的一半低于零,1 个可能的数字是 =(零),其余 127 个数字高于 0。
The first bit is the sign. (1 - minus, 0 - plus)
第一位是符号。(1 - 减,0 - 加)
回答by LuigiEdlCarno
Formula for Range calculation is : -2^(n-1) to (2^(n-1)-1)
范围计算公式为:-2^(n-1) 到 (2^(n-1)-1)
where n = no. of bits of primitive datatype. Example:
其中 n = 否。原始数据类型的位。例子:
For int datatype, n is 32, in short datatype, n is 16 etc.
对于 int 数据类型,n 为 32,简而言之,n 为 16 等。
So, int range will be: -2^(32-1) to (2^(32-1)-1)
所以,int 范围将是:-2^(32-1) 到 (2^(32-1)-1)
By using the same formula Range of byte, short, float and double could be calculated.
通过使用相同的公式可以计算字节、短、浮点和双精度的范围。
回答by Jayani Sumudini
Let's calculate range for 1 Byte
让我们计算 1 个字节的范围
- 1 bit can take 0 or 1
- 1 Byte = 8 Bits
- The first bit is used as a sign ( - or + )
- then remaining bits are 7
- so we can write 2^7 = 128 different numbers for one sign
- we get 0 as a positive sign. then we have 128 numbers for the negative side,127 numbers for the positive side and 0 (zero)
- so the range is -128 to 127 including 0
- 1 位可以取 0 或 1
- 1 字节 = 8 位
- 第一位用作符号( - 或 + )
- 然后剩下的位是 7
- 所以我们可以为一个符号写出 2^7 = 128 个不同的数字
- 我们得到 0 作为一个正号。那么我们有 128 个负数,127 个正数和 0(零)
- 所以范围是-128 到 127 包括 0
回答by Agustin Meriles
That's because the first bit is used for the sign, since the data type is signed.
那是因为第一位用于符号,因为数据类型是有符号的。
Please refer to http://en.wikipedia.org/wiki/Signed_number_representations
请参考http://en.wikipedia.org/wiki/Signed_number_representations
Since there is no unsigned
primitive types in Java (like C or C#), is usual to cast it to a bigger type if you need to "overflow" the boundaries.
由于unsigned
Java 中没有原始类型(如 C 或 C#),如果您需要“溢出”边界,通常会将其转换为更大的类型。
回答by Cute Pari
Formula to calculate range in Java
-2(n-1)to +2(n-1)-1
Where n is the number of bit (1 byte= 8 bit)
Java中计算范围的公式
-2 (n-1)到 +2 (n-1)-1
其中 n 是位数(1 字节 = 8 位)
So, for byte type range would be:
-2(8-1)to +2(8-1)-1
or, -2(7)to +2(7)-1
or, -128 to +127
因此,对于字节类型范围将是:-2 (8-1)到 +2 (8-1)-1
或 -2 (7)到 +2 (7)-1
或 -128 到 +127
回答by Manpreet Saini
The last bit i.e. number 8 we are writing it 2^7 is a sign bit that decides negative or positive sign so it is 2^0 +2^1 +2^2 +2^3 +2^4 +2^5+ 2^6
最后一位,即我们写的数字 8 2^7 是决定负号或正号的符号位,所以它是 2^0 +2^1 +2^2 +2^3 +2^4 +2^5+ 2^6
回答by rich
it results from the standard representation of signed values as binary numbers. For a full description see http://en.wikipedia.org/wiki/Two%27s_complement.
它是由带符号值作为二进制数的标准表示产生的。有关完整说明,请参阅http://en.wikipedia.org/wiki/Two%27s_complement。
-Rich
-富有的
回答by Suganth S
Bit consists of 0's and 1's.bytenormally consists of 8 bits.so the values can be calculated using the general formula which is given below,
位由 0 和 1 组成。字节通常由 8 位组成。因此可以使用下面给出的通用公式计算这些值,
no of values data type can have=2^n(2 power n)
, where n represents no of bits.
so the value of byte data type=2^8(i.e 1 byte=8 bits)
,here n=8
byte value=256
no of values 数据类型可以有= 2^n(2 power n)
,其中 n 表示没有位数。所以字节数据类型的值= 2^8(i.e 1 byte=8 bits)
,这里n=8
字节值=256
And it should be shared equal on both sides of zero ( half values at negative and half value at positive ). Hence the range for byte is from -128 to 127.
并且它应该在零的两侧相等(负值的一半和正值的一半)。因此字节的范围是从 -128 到 127。
回答by rahul
Range of data types so now we came to know that how we are calculating the Range of the integer data types.This logic is applicable for all the integer data types.
数据类型的范围所以现在我们开始知道我们如何计算整数数据类型的范围。这个逻辑适用于所有整数数据类型。
The full details of all the data types are given below,
下面给出了所有数据类型的完整详细信息,
S.NO Data Type Bits Ranges values
S.NO 数据类型位范围值
1 boolean 1 – true or false(1 or 0)
1 布尔值 1 – 真或假(1 或 0)
2 byte 8 -128 to 127. 256(2^8)
2 字节 8 -128 到 127。256(2^8)
3 short 16 -32,768 to 32,767 65,536(2^16)
3 短 16 -32,768 到 32,767 65,536(2^16)
4 int 32 -2^31 to (2^31)-1 2^32
4 int 32 -2^31 到 (2^31)-1 2^32
5 long 64 Refer NOTE 2^64
5长64参考NOTE 2^64
6 float 32 Refer NOTE ———
6 浮点数 32 参考注意——
7 double 64 Refer NOTE ———-
7 双 64 参考注意————
8 char 16 0 to 65,535 65,536(2^16)
8 个字符 16 0 到 65,535 65,536(2^16)