使用无符号操作数的类型转换

编程入门 行业动态 更新时间:2024-10-11 13:28:09
使用无符号操作数的类型转换-C(Type conversion with unsigned operands- C)

在第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.

更多推荐

本文发布于:2023-07-30 16:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   类型   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!