C ++最大非负整数 [英] C++ maximum non negative int
本文介绍了C ++最大非负整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是否会在所有平台上正常工作,int大小等?还是有更加公认的做法吗? (我做了以下。)
Is the following going to work as expected on all platforms, sizes of int, etc? Or is there a more accepted way of doing it? (I made the following up.)
#define MAX_NON_NEGATIVE_INT ((int)(((unsigned int)-1) / 2))
我不会通过解释它的作用来侮辱你的智力!
I won't insult your intelligence by explaining what it's doing!
编辑:我应该提到我不能使用任何标准类,因为我没有C运行时。
I should have mentioned that I cannot use any standard classes, because I'm running without the C runtime.
推荐答案
如果你不想使用定义(并且你想要一个标准的计算限制的方法),然后这样做:
If you don't want to use defines (and you want a standard way of calculating the limits), then do this:
#include <limits>
std::numeric_limits<int>::min()
定义在limits.h中:
These are the ANSI standard defines in limits.h:
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
#define UINT_MAX 0xffffffff /* maximum unsigned int value */
这些是来自BaseTsd.h的定义:
These are the defines from BaseTsd.h:
#define MAXUINT ((UINT)~((UINT)0))
#define MAXINT ((INT)(MAXUINT >> 1))
#define MININT ((INT)~MAXINT)
这篇关于C ++最大非负整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文