厌倦了使用-1表示最大无符号值 [英] Weary of using -1 for max unsigned values

查看:91
本文介绍了厌倦了使用-1表示最大无符号值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我通常使用-1表示最大无符号值,或者像-7一样使用
来获得最大值的6;有点像:


unsigned x = ...


if(-7 == x)puts(" x is 6 away)从它的最大值);


因为我很少使用小于int的东西,所以这从来就不是一个问题。显然,如果我尝试的话会有问题:


char unsigned x = ...


if(-7 = = x)put(x是6远......


出现问题的原因是x可以提升为

SIGNED int。


所以我想知道......对于任何

无符号整数表达式,检查这个的最方便的方法是什么?


如果是unsigned char,我们可以做

if((char unsigned)-7 == x)


同样在unsigned short的情况下,我们可以做

if((short unsigned)-7)== x)


但是我是什么我正在寻找的是一种通用的方法。我的第一个想法是

类似


#define MAX(expr)((expr)*(char_unsigned)0 - (char

unsigned)1)


当然这个问题是,即使我们有演员的
,一切仍然会被提升为int (签名或

未签名取决于INT_MAX)。


任何人都有任何想法,我可以编写通用代码,如?:


如果(MAX(x)-6 == x)put(x与其最大值相差6);


如果我们有一个typeof操作员它可能是这样的:


#define MAX(expr)((typeof expr)-1)


我喜欢-1 == x的在我的代码中散落......并且是的

它工作得很好......但我想要更具可持续性以防万一

我会选择使用小于int的东西。


Martin


I commonly use the likes of -1 for max unsigned values, or something
like -7 to get 6 away from the max value; sort of like:

unsigned x = ...

if (-7 == x) puts("x is 6 away from its maximum");

Since I rarely use anything smaller than int, this has never been a
problem. Obviously though, there''d be a problem if I tried:

char unsigned x = ...

if (-7 == x) puts("x is 6 away...

The reason there''d be a problem is that x could be promoted to a
SIGNED int.

So I''m wondering... what''s the handiest way of checking this for any
unsigned integer expression?

In the case of unsigned char, we can do
if ((char unsigned)-7 == x)

Similarly in the case of unsigned short, we can do
if ((short unsigned)-7) == x)

But what I''m looking for is a universal method. My first thoughts were
something like

#define MAX(expr) ( (expr) * (char_unsigned)0 - (char
unsigned)1 )

The problem with this of course though is that, even though we have
the casts, everything will still get promoted to int (either signed or
unsigned depending on INT_MAX).

Anyone got any ideas such that I can write universal code such as?:

if (MAX(x)-6 == x) puts("x is 6 away from its maximum");

If we had a typeof operator it would probably be something like:

#define MAX(expr) ( (typeof expr)-1 )

I have the likes of "-1 == x" littered throughout my code... and yes
it works fine... but I''d like something more sustainable just in case
I''d choose to use something smaller than an int.

Martin

推荐答案

Martin Wells写道:
Martin Wells wrote:

我通常使用-1的最大无符号值,或者像-7这样的
来获得最大值的6;有点像:


unsigned x = ...


if(-7 == x)puts(" x is 6 away)从它的最大值);


因为我很少使用小于int的东西,所以这从来就不是一个问题。显然,如果我尝试的话会有问题:


char unsigned x = ...


if(-7 = = x)put(x是6远......


出现问题的原因是x可以提升为

SIGNED int。


所以我想知道......对于任何

无符号整数表达式,检查这个的最方便的方法是什么?
I commonly use the likes of -1 for max unsigned values, or something
like -7 to get 6 away from the max value; sort of like:

unsigned x = ...

if (-7 == x) puts("x is 6 away from its maximum");

Since I rarely use anything smaller than int, this has never been a
problem. Obviously though, there''d be a problem if I tried:

char unsigned x = ...

if (-7 == x) puts("x is 6 away...

The reason there''d be a problem is that x could be promoted to a
SIGNED int.

So I''m wondering... what''s the handiest way of checking this for any
unsigned integer expression?



if(UINT_MAX-6 == x)...


将UINT_MAX替换为UCHAR_MAX,USHRT_MAX,ULONG_MAX,ULLONG_MAX,或者

SIZE_MAX,视情况而定。

if (UINT_MAX-6 == x ) ...

Replace UINT_MAX with UCHAR_MAX, USHRT_MAX, ULONG_MAX, ULLONG_MAX, or
SIZE_MAX, as appropriate.


james:
james:

if(UINT_MAX-6 == x)...

将UINT_MAX替换为UCHAR_MAX,USHRT_MAX,ULONG_MAX,ULLONG_MAX或

SIZE_MAX,视情况而定b $ b
if (UINT_MAX-6 == x ) ...

Replace UINT_MAX with UCHAR_MAX, USHRT_MAX, ULONG_MAX, ULLONG_MAX, or
SIZE_MAX, as appropriate



使用TYPE_MAX并不比(TYPE)-1更通用。我正在寻找

我能用于任何无符号整数的东西类型。


Martin


Using TYPE_MAX is no more universal than (TYPE)-1. I''m looking for
something I can use with any unsigned integer type.

Martin


Martin Wells你想出了最愚蠢的问题。

first memset,现在这个..

Martin Wells you come up with the silliest problems.
first memset, now this..


这篇关于厌倦了使用-1表示最大无符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆