在第44页,K&R的C编程语言中,它在底部说:
例如,假设int是16位而long是32位。 然后-1L <1U,因为1U是一个int,被提升为有符号长整数。 但-1L> 1UL,因为-1L被提升为无符号长,因此看起来是一个大的正数。
我真的不知道那是在说什么。 我认为这是比较比特,但我真的不知道。 你能描述一下这是在讨论什么吗?
PS我是C的真正的老兄.C是我的第一个编程语言。 那么,请你尽可能简单地回答这个问题吗? 谢谢。
In pg 44, C programming language by K&R, at the bottom it said:
For example, suppose that int is 16 bits and long is 32 bits. Then -1L < 1U, because 1U, which is an int, is promoted to a signed long. But -1L > 1UL, because -1L is promoted to unsigned long and thus appears to be a large positive number.
I really don't know what that is talking about. I think it's comparing bits but I really don't know. Can you describe what this is talking about please?
P.S. I am really noob at C. C is my first programming language. So, can you please answer this question as easy as possible? Thank you.
最满意答案
在C中,相同的基本位0xffff具有两个不同的值。
如果将16位数字视为“有符号”,则0xffff为-1,如果为“无符号”,则0xffff为65535。
将有符号值与无符号值进行比较时,有符号值中的位将被视为“无符号”,如果值为负,则新值可能不是您想要的值。
In C, the same basic bits 0xffff has two different values.
If you treat the 16-bit number as "signed" then 0xffff is -1, if it is "unsigned" then 0xffff is 65535.
When you compare a signed value with an unsigned value, the bits in the signed value get treated as "unsigned" and if the value was negative, the new value is probably not what you wanted.
更多推荐
发布评论