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

元气小坏坏 提交于 2021-02-05 20:28:53

问题


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.

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.

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:

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

回答1:


std::numeric_limits<T>

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

  • T std::numeric_limits<T>::min()
  • T std::numeric_limits<T>::max()

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

Formulas

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

  • min = -1 * 2N - 1
  • max = 2N - 1 - 1

The formulas for an unsigned number with N bits are

  • min = 0
  • max = 2N - 1

Example: 8-bit char

The char has N = 8 bits. Let's verify these formulas with signed char and 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



回答2:


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.

What matters is the number of bits. If you have n bits, then you can express 2n different 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 0 to 65535.

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 0 in the middle, and then you split the remaining values between negatives and positives. So you get -32768 on the low end and 32767 on the high end.

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



来源:https://stackoverflow.com/questions/18112281/formula-to-determine-the-range-of-signed-and-unsigned-data-types-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!