无符号整数的陷阱表示 [英] Trap representations for unsigned integers

查看:75
本文介绍了无符号整数的陷阱表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果uMyInt_t是无符号整数类型,则以下是

必要且充分条件,uMyInt_t没有陷阱

表示?


(uMyInt_t)( - 1)> CHAR_BIT * sizeof(uMyInt_t)-1


也就是说,只要uMyInt_t有陷阱,我就会问它等于0 br />
表示,只要uMyInt_t没有


-

#include< stdio.h>

char s [] =" \ 16Jsa ukenethr,cto haCr \ n" ;; int main(void){* s * = 5; *

s%= 23; putchar(s [0] [s]); return * s-14?main():! putchar(9 [s + * s]);}

If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeof(uMyInt_t)-1

That is, I''m asking wheter it equals 0 whenever uMyInt_t has trap
representations, equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.

--
#include <stdio.h>
char s[]="\16Jsa ukenethr ,cto haCr\n";int main(void){*s*=5;*
s%=23;putchar(s[0][s]);return*s-14?main():!putchar(9[s+*s]);}

推荐答案

Army1987写道:
Army1987 wrote:

如果uMyInt_t是无符号整数类型,则如下a

必要且充足的条件,uMyInt_t没有陷阱

代表?


(uMyInt_t)( - 1 )> CHAR_BIT * sizeof(uMyInt_t)-1
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeof(uMyInt_t)-1



编号如果uMyInt_t有填充位,你将右移一个数字

大于(或等于)值位数,为此

行为未定义。

No. If uMyInt_t has padding bits, you will right-shift by a number
greater than (or equal to) the number of value bits, and for that the
behaviour is undefined.


Army1987写道:
Army1987 wrote:

如果uMyInt_t是无符号整数类型,则以下是

必要且充分条件,即uMyInt_t没有陷阱
代表?


(uMyInt_t)( - 1)> CHAR_BIT * sizeof(uMyInt_t)-1


即,每当uMyInt_t有陷阱时,我都会问它等于0

表示,只要uMyInt_t没有
陷阱表示,就等于非零值,并且永远不会触发未定义的行为。
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeof(uMyInt_t)-1

That is, I''m asking wheter it equals 0 whenever uMyInt_t has trap
representations, equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.



我认为此测试至少存在两个问题。

首先,如果uMyInt_t有填充位,则移位计数可能是

太大并导致未定义的行为(可能因为

可能提升为int或unsigned int)。其次,填充位的存在并不意味着存在陷阱

表示:额外的位可能只是为了骑行。

检测填充位的最佳方法可能是计算(uMyInt_t)-1中的1'的
数,或者比较数值

的(uMyInt_t)-1到预期的数量。第一次测试是在运行时轻松获得
但在预处理时很难或不可能

时间;第二个也有问题(你应该使用什么类型

来形成预期值?)。我认为没有可靠的方法来确定是否存在陷阱表示;如果你发现

没有填充位,你可以推断出没有

陷阱,但这就是我认为你能得到的。


-

Eric Sosman
es * ****@acm-dot-org.inva 盖子

I think there are at least two problems with this test.
First, if uMyInt_t has padding bits the shift count may be
too large and lead to undefined behavior ("may" because of
possible promotion to int or unsigned int). Second, the
presence of padding bits does not imply the existence of trap
representations: the extra bits may just be along for the ride.

The best way to detect padding bits may be to count the
number of 1''s in (uMyInt_t)-1, or to compare the numeric value
of (uMyInt_t)-1 to the "expected" quantity. The first test is
easy at run time but difficult or impossible at preprocessing
time; the second has problems, too (what type should you use
to form the expected value?). I can think of no reliable way
to determine whether trap representations exist; if you find
there are no padding bits you can deduce that there are no
traps, but that''s as far as I think you can get.

--
Eric Sosman
es*****@acm-dot-org.invalid


" Harald van D?k" < tr ***** @ gmail.comha scritto nel messaggio

news:11 ********************** @ b58g2000hsg .googlegr oups.com ...
"Harald van D?k" <tr*****@gmail.comha scritto nel messaggio
news:11**********************@b58g2000hsg.googlegr oups.com...

Army1987写道:
Army1987 wrote:

>如果uMyInt_t是无符号的积分型,是以下一个充分必要条件,uMyInt_t没有陷阱表示吗?

(uMyInt_t)( - 1)> CHAR_BIT * sizeof(uMyInt_t) - 1
>If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeof(uMyInt_t)-1



编号如果uMyInt_t有填充位,你将右移一个数字

大于(或等于)数字值位,为此

行为未定义。


No. If uMyInt_t has padding bits, you will right-shift by a number
greater than (or equal to) the number of value bits, and for that the
behaviour is undefined.






(DBL_MAX> =(uMyInt_t)( - 1)||(puts(" Your DS9K)即将自我 - \

destruct。获得一台真正的电脑。),退出(1)),

/ *在DeathStation 9000出口(1)激活自我毁灭* /

ceil(log2((uMyInt_t)( - 1)))> = CHAR_BIT * sizeof(uMyInt_t))


any更好?

Is

( DBL_MAX >= (uMyInt_t)(-1) || (puts("Your DS9K is about to self-\
destruct. Get a real computer."), exit(1)),
/* On the DeathStation 9000 exit(1) activates self-destruction */
ceil(log2((uMyInt_t)(-1))) >= CHAR_BIT*sizeof(uMyInt_t) )

any better?


这篇关于无符号整数的陷阱表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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