如何判断一个变量是未签名或不在ANSI C [英] How to determine whether a variable is unsigned or not in ANSI C

查看:89
本文介绍了如何判断一个变量是未签名或不在ANSI C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C专家编程由彼得·林登。第二章A.6中,writter描述了如何确定一个变量是否是未签名或不在K&放大器; R C.The宏如下:

I am learning "Expert C Programming" by Peter Van Der Linden. In chapter A.6, the writter described how to determine whether a variable is unsigned or not in K&R C.The macro is below:

#define ISUNSIGNED(a) (a>=0 && ~a>=0)

这本书是很老,它最早于1994年出版!前R C;而我还没学会K&放大器。
现在的问题是,如何判断一个变量是否是未签名或不在ANSI C

The book is very old, it was first published in 1994! And I have not learned K&R C before. The question is that how to determine whether a variable is unsigned or not in ANSI C.

我试图来解决这样的问题。由于0是ANSI C int和浮点除外的其他数字,double和long double将被转换成int或unsigned int由整型升级时以0比所以我想找到无符号和符号数之间的边缘。当我比较(边缘型)0用,a的类型将不被改变。宏也模型如下:

I have tried to solve the problem like this. Since "0" is int in ANSI C, and any other number except float, double and long double will be converted to int or unsigned int by Integer Upgrade when compare with 0. So I want to find an edge between unsigned and signed number. When I compare (the edge type)0 with a, the type of a will not be changed. The macro also the model is below:

#define ISUNSIGNED(a) (a>=(the edge type)0 && ~a>=(the edge type)0)

我找不到边缘的类型,有没有人可以帮我解决这个问题?
我已经改变数量到变量更精确的前pression。

I can not find the edge type, is there anybody can help me solve the problem? I have changed "number" to "variable" for more accurate expression.

推荐答案

一个符号变量有其标志存储的部分的位。通常这是最显著之一,但它可以是其中任何一个。无符号的变量没有符号位;因此,它可以容纳的最低值是0。这意味着,对于一个无符号的变量 A ,前pression A> = 0 总是的是真实的。

How this works

A signed variable has to store its sign in some bit. Usually this is the most significant one, but it could be any of them. An unsigned variable has no sign bits; thus, the lowest value it can hold is 0. This means that for an unsigned variable a, the expression a >= 0 will always be true.

因此​​,我们有:

( a >= 0 && ~a >= 0 )

如果 A 是无符号的,第一个是真实的(它是),第二个是真实的(因为任何值〜一个是,它仍然是一个无符号的价值,所以它仍然是> = 0 )。如果 A 签署,这意味着,如果标志置位, A> = 0 为假(和恩pression返回false,说明这个变量有一个符号类型)。如果符号位不设置 A ,那么当〜一个反转的所有的在的位,符号位(这是哪一个)的的进行设置。这意味着,它必须是一个负数,这意味着〜一方式> = 0 返回假

If a is unsigned, the first is true (it has to be), and the second is true (because whatever value ~a is, it's still an unsigned value, so it's still >= 0). If a is signed, that means that if the sign bit is set, a >= 0 is false (and the expression returns false, stating that this variable has a signed type). If the sign bit isn't set in a, then when ~a inverts all the bits in a, the sign bit (whichever one it is) has to be set. This means that it has to be a negative number, which means that ~a >= 0 returns false.

这不依赖于标准的整数促销工作像你指望他们。

This does rely on the standard integer promotions to work like you'd expect them to.

unsigned char x = 1; // or whatever

printf("%s\n", ISUNSIGNED(x) ? "TRUE" : "FALSE"); // prints "FALSE"

至于其他人指出的那样, unsigned char型因为任何值提升为一个 INT 〜一个 unsigned char型A 可以很容易适应的范围 INT 。这可以说是标准的整数促销活动失败(或积分文字的打字不及格)。

As someone else pointed out, unsigned char gets promoted to an int since any value of ~a for an unsigned char a can easily fit in the range of an int. This is arguably a failing in the standard integer promotions (or a failing in the typing of integral literals).

有可能是 ISUNSIGNED 的另一种实现或 ISSIGNED 的地方,可以克服这个限制。该 P99宏库有宏的一些令人费解的用途,许多依靠C99的可变参数宏,但不幸的是,宏来检查前pression是否签署或不(的#define符号(表达式)((1 -1:表达式)≤(1 0:表达式)))屈从于相同的整数促销。这可能是你能做的最好的(虽然我想这总比没有的情况下,你会希望它更好)。

There might be another implementation of ISUNSIGNED or ISSIGNED somewhere that can overcome this limitation. The P99 macro library has some mind-bending uses of macros, many relying on C99's variadic macros, but unfortunately the macro to check whether an expression is signed or not (#define SIGNED(expr) ((1 ? -1 : expr) < (1 ? 0 : expr))) succumbs to the same integer promotions. This might be the best you can do (though I suppose it's better than nothing in the cases where you'll want it).

这篇关于如何判断一个变量是未签名或不在ANSI C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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