检测填充位的最佳方法可能是计算(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 betoo large and lead to undefined behavior ("may" because ofpossible promotion to int or unsigned int). Second, thepresence of padding bits does not imply the existence of traprepresentations: the extra bits may just be along for the ride.The best way to detect padding bits may be to count thenumber of 1''s in (uMyInt_t)-1, or to compare the numeric valueof (uMyInt_t)-1 to the "expected" quantity. The first test iseasy at run time but difficult or impossible at preprocessingtime; the second has problems, too (what type should you useto form the expected value?). I can think of no reliable wayto determine whether trap representations exist; if you findthere are no padding bits you can deduce that there are notraps, but that''s as far as I think you can get.--Eric Sosman es*****@acm-dot-org.invalid 编号如果uMyInt_t有填充位,你将右移一个数字 大于(或等于)数字值位,为此 行为未定义。No. If uMyInt_t has padding bits, you will right-shift by a numbergreater than (or equal to) the number of value bits, and for that thebehaviour 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? 这篇关于无符号整数的陷阱表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 05:44