确定有符号和无符号数据类型范围的公式 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18112281/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:43:48  来源:igfitidea点击:

Formula to determine the range of signed and unsigned data types C++

c++math

提问by Andrew B

I'm just starting out in C++ (literally my second day) and I've been assigned to calculate the ranges of the varying data types, signed and unsigned. The problem is, the way my school works I won't get to the Math portion where he teaches us the formula for another couple months. He said to get the information from someone who has done the math course already however all of them said they're going to work on this from home where they have their notes. So now I'm left in the dark with google and its inconclusive answers, so I ask you, the wise folk of stackoverflow.

我刚开始使用 C++(实际上是我的第二天),我被分配计算不同数据类型(有符号和无符号)的范围。问题是,我学校的工作方式我不会再进入数学部分,他再教我们几个月的公式。他说要从已经完成数学课程的人那里获取信息,但是他们所有人都说他们将在家里做笔记,并在那里做这件事。所以现在我对谷歌及其不确定的答案一无所知,所以我问你,stackoverflow 的聪明人。

What are the formulas for getting the range of the data types? I have found one for INT that has worked but does not apply to the others, the ones he wants us to calculate are: char, short, long, and long long. He also wants the unsigned versions of those 4 as well as INT.

获取数据类型范围的公式是什么?我找到了一个适用于 INT 的方法,它有效但不适用于其他方法,他希望我们计算的是:char、short、long 和 long long。他还想要这 4 个的未签名版本以及 INT。

We already have the size in bits and bytes of each of these data types.

我们已经有了每种数据类型的位和字节大小。

Here is how I have my INT range laid out:

这是我如何布置我的 INT 范围:

    printf ("\nThe range of int is: %f\n", pow(2, (sizeof(int) * CHAR_BIT) -1));

回答by Timothy Shields

std::numeric_limits<T>

std::numeric_limits<T>

You can get the actual values of these limits using these functions:

您可以使用以下函数获取这些限制的实际值:

You substitute the appropriate type in place of T, such as signed charor unsigned char.

您可以用适当的类型代替T,例如signed charunsigned char

Formulas

公式

The formulas for a signed number with N bits (using two's complement) are

N 位有符号数的公式(使用二进制补码)是

  • min = -1 * 2N - 1
  • max = 2N - 1- 1
  • 最小值 = -1 * 2 N - 1
  • 最大值 = 2 N - 1- 1

The formulas for an unsigned number with N bits are

N 位无符号数的公式是

  • min = 0
  • max = 2N- 1
  • 最小值 = 0
  • 最大值 = 2 N- 1

Example: 8-bit char

示例:8 位 char

The charhas N = 8 bits. Let's verify these formulas with signed charand unsigned char.

char具有N = 8位。让我们用signed char和验证这些公式unsigned char

  • signed char
    • min = -1 * 2N - 1= -1 * 27= -128
    • max = 2N - 1- 1 = 27- 1 = 127
  • unsigned char
    • min = 0
    • max = 2N- 1 = 28- 1 = 255
  • signed char
    • 最小值 = -1 * 2 N - 1= -1 * 2 7= -128
    • 最大值 = 2 N - 1- 1 = 2 7- 1 = 127
  • unsigned char
    • 最小值 = 0
    • 最大 = 2 N- 1 = 2 8- 1 = 255

回答by DUman

In actuality, those ranges can be found in <limits>- see reference, and specifically the template std::numeric_limits- see here. I'm assuming though that you want to know where those numbers come from.

实际上,这些范围可以在<limits>- 请参阅参考资料,特别是模板std::numeric_limits- 请参阅此处。我假设您想知道这些数字来自哪里。

What matters is the number of bits. If you have nbits, then you can express 2ndifferent values with them. Take an old-school 16-bit integer. With 16 bits, the amount of possible different values is 216= 65536. If you have an unsigned value, you're only expressing non-negative values. So the smallest value you express is 0, and the largest is 2n- 1, so a 16-bit unsigned integer goes from 0to 65535.

重要的是位数。如果你有n位,那么你可以用它们表达 2 n 个不同的值。取一个老式的 16 位整数。对于 16 位,可能的不同值的数量是 2 16= 65536。如果您有一个无符号值,则您仅表示非负值。所以你表达的最小值是 0,最大值是 2 n- 1,所以一个 16 位无符号整数从065535

If you are expressing integers with a sign, then of course you now have the same amount of values (65536 in the previous example) to express some positive and negative numbers. You've got 0in the middle, and then you split the remaining values between negatives and positives. So you get -32768on the low end and 32767on the high end.

如果您用符号表示整数,那么当然您现在有相同数量的值(在前面的示例中为 65536)来表示一些正数和负数。你已经0在中间,然后你在负数和正数之间分割剩余的值。所以你-32768在低端和32767高端。

For any other datatype that represents an integer, it's the same pattern, whether it's 32, 64 or more bits.

对于表示整数的任何其他数据类型,无论是 32 位、64 位还是更多位,都是相同的模式。