符号和无符号和位扩展用C如何工作的 [英] Signed and unsigned, and how bit extension works in C

查看:187
本文介绍了符号和无符号和位扩展用C如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned short s;
s = 0xffff;
int i = s;

如何扩展在这里工作? 2更大的订单字节被添加,但我很困惑1或0是否延长在那里。这可能是依赖于平台让我们专注于Unix的一样。请问INT两个较大的顺序字节填充1或0,为什么?

How does the extension work here? 2 larger order bytes are added, but I'm confused whether 1's or 0's are extended there. This is probably platform dependent so let's focus on what Unix does. Would the two bigger order bytes of the int be filled with 1's or 0's, and why?

基本上没有电脑知道取值是无符号,并正确地分配0为int的高阶位?因此, I 现在 0x0000ffff ?或因为整数都默认在UNIX中签没有考虑从的签订位S (1),并复制到高阶字节?

Basically, does the computer know that s is unsigned, and correctly assign 0's to the higher order bits of the int? So i is now 0x0000ffff? Or since ints are default signed in unix does it take the signed bit from s (a 1) and copy that to the higher order bytes?

推荐答案

没有,一个无符号值永远不会符号扩展。上溯造型永远垫这样用零值。

No, an unsigned value is never sign-extended. Upcasting will always pad such a value with zeroes.

更多precisely,无符号变量重新presents特定数量,它仍然会重新present相同数量的铸造后,只要是有可能在新的格式。

More precisely, the unsigned variable represents a particular number, and it will still represent the same number after a cast, provided that is possible in the new format.

十六进制或无,C(虽然不是C99)和C ++的设计在没有位的,例如,使用基10 Numerics的工作。

Hexadecimal or no, C (although not C99) and C++ are designed to work in the absence of bits, eg with base-10 numerics.

这篇关于符号和无符号和位扩展用C如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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