我无法弄清楚如何计算最大和最小有符号数而不使用C ++中的climbits

时间:2015-01-17 04:48:15

标签: c++ unsigned signed

我正在为我的CS课程做作业。我正在尝试打印数据类型int,short和long的最大和最小有符号数。

我不完全确定如何做到这一点。我一直在使用sizeof()函数来确定每种数据类型携带的字节数,然后使用来自cmath库的pow()。

这是我用来计算签名短片最大数量的数学/代码: (int)pow(2,sizeof(short)* 8)-1;

输出最大无符号数,而不是最大有符号数。我可以把它除以2,但不完全确定如何计算负面部分,而不是在前面用“ - ”打印..

我现在所拥有的更详细的代码:

 42     int shortmax_calculate;
 43     shortmax_calculate = (pow(2, sizeof(short)*8)-1)/2;
 44
 45     float shortmin_calculate;
 46     shortmin_calculate = (int)pow(2, sizeof(short)*8)/2;
 47
 48     short unsigned shortunsigned_calculate;
 49     shortunsigned_calculate = pow(2, sizeof(short)*8)-1;
 50
 51     int intmax_calculate;
 52     intmax_calculate = (int)pow(2, sizeof(int)*8)-1;
 53
 54
 55     cout << "Maxmimum short (signed): "<< shortmax_calculate << endl;
 56     cout << "Minimum short (signed): " << "-" << shortmin_calculate << endl;
 57     cout << "Maximum short (unsigned): " << shortunsigned_calculate << endl;
 58     cout << "Maximum int (signed): " << intmax_calculate << endl;

感谢任何帮助,谢谢。

4 个答案:

答案 0 :(得分:1)

假设您可以使用C ++ 11,请使用limits标准标题:

#include <iostream>
#include <limits>
...

{
    std::cout << "Maxmimum short (signed): " <<
        std::numeric_limits<short>::max() << std::endl;

    // repeat replacing with 'unsigned short', 'int', 'unsigned int'
    // in template class function: std::numeric_limits<type>, using
    // max() or min() as required.
}

查看clang's实施情况 - 搜索__libcpp_numeric_limits以了解其评估方式。如果你不能使用C ++ 11,它可能会给你一些模板的想法。

答案 1 :(得分:0)

一些模板代码完成了这项工作。

#include <iostream>
#include <cstdint>

template <size_t S> struct Max
{
   static intmax_t get()
   {
      return ((Max<S-1>::get() << 8) + 0xFF);
   }
};

template <> struct Max<1>
{
   static intmax_t get()
   {
      return 0x7F;
   }
};

template <size_t S> struct Min
{
   static intmax_t get()
   {
      return ((Min<S-1>::get() << 8));
   }
};

template <> struct Min<1>
{
   static intmax_t get()
   {
      return -0x80;
   }
};

int main()
{
   std::cout << "Max for int: " << Max<sizeof(int)>::get() << std::endl;
   std::cout << "Min for int: " << Min<sizeof(int)>::get() << std::endl;
   std::cout << "Max for short: " << Max<sizeof(short)>::get() << std::endl;
   std::cout << "Min for short: " << Min<sizeof(short)>::get() << std::endl;
   std::cout << "Max for signed char: " << Max<sizeof(signed char)>::get() << std::endl;
   std::cout << "Min for signed char: " << Min<sizeof(signed char)>::get() << std::endl;
}

这是我在我的机器上得到的:

Max for int: 2147483647
Min for int: -2147483648
Max for short: 32767
Min for short: -32768
Max for signed char: 127
Min for signed char: -128

答案 2 :(得分:0)

所以我认为你要找的只是添加一个你计算的最大值。我在下面修改了你的代码(在C抱歉)。

  short shortmax_calculate;
  shortmax_calculate = (pow(2, sizeof(short)*8)-1)/2;

  short shortmin_calculate;
  shortmin_calculate = shortmax_calculate + 1;

  short unsigned shortunsigned_calculate;
  shortunsigned_calculate = pow(2, sizeof(short)*8)-1;

  int intmax_calculate;
  intmax_calculate = (int)pow(2, sizeof(int)*8)-1;
  int intmin_calculate = intmax_calculate + 1;

  printf("Maximum short (signed): %i\n",shortmax_calculate);
  printf("Minimum short (signed): %i\n",shortmin_calculate);
  printf("Maximum short (unsigned): %i\n", shortunsigned_calculate);
  printf("Maximum int (signed): %i\n", intmax_calculate);
  printf("Minimum int (signed): %i\n", intmin_calculate);

以下是代码的输出:

Maximum short (signed): 32767 
Minimum short (signed): -32768
Maximum short (unsigned): 65535 
Maximum int (signed): 2147483647 
Minimum int (signed): -2147483648

这是因为计算机存储负数。例如:max short(signed)是01111111,即127. Min签名存储为10000000(-128)。有关详细信息,请参阅此link

答案 3 :(得分:0)

在二进制补码中,-1将设置所有位。如果将其转换为无符号并将其右移1,则它将是最大可能的有符号数,因为符号位将被替换为零。如果你采用它的按位反转,它将是可能的最小负数。

short shortmax_calculate = static_cast<short>(static_cast<unsigned short>(-1) >> 1);
short shortmin_calculate = ~shortmax_calculate;