有符号整数转换为无符号整数 [英] conversion of signed integer to unsigned integer

查看:260
本文介绍了有符号整数转换为无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释一下:


[N869 6.3.1.3]


当整数类型的值转换为另一个整数时类型

除了_Bool,

如果新类型是无符号的,则重复转换值

加上或减去一个以上的最大值值可以是

在新类型中表示,直到该值在新的

类型的范围内。

Thanx ...

Can anybody please explain this:

[ N869 6.3.1.3 ]

When a value with integer type is converted to another integer type
other than _Bool,
if the new type is unsigned, the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type.
Thanx ...

推荐答案

ju**********@yahoo.co.in 写道:
ju**********@yahoo.co.in wrote:
任何人都可以解释一下:

[ N869 6.3.1.3]

当一个整数类型的值被转换为另一个整数类型
而不是_Bool时,如果新类型是无符号的,则值将被重复转换
加上或减去一个可以在新类型中表示的最大值,直到值为止在新的
类型的范围内。
Can anybody please explain this:

[ N869 6.3.1.3 ]

When a value with integer type is converted to another integer type
other than _Bool,
if the new type is unsigned, the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type.




unsigned short us; / *假设USHRT_MAX == 65535 * /

us = -1000000;


变量是无符号的,无法表达

负值。赋值必须将其设置为非负的
值,6.3.1.3描述如何计算该值:


-1000000

+65536(USHRT_MAX + 1)

========

-934464

+65536

========

-868928





== ======

-16960

+65536

========

48576(最终答案,Regis)


提供各种计算快捷方式;所有那些

添加物实际上不需要实现以获得答案。


-

Eric Sosman
es*****@acm-dot-org.inva





Eric Sosman写道:


Eric Sosman wrote:
ju ********** @ yahoo.co.in 写道:
ju**********@yahoo.co.in wrote:
任何人都可以解释一下:

[N869 6.3.1.3]

当整数类型的值转换为另一个整数类型
而不是_Bool时,如果新类型是无符号的,则
,该值通过重复加/减一个可以在新类型中表示的最大值来转换,直到该值在新的
类型的范围内。
Can anybody please explain this:

[ N869 6.3.1.3 ]

When a value with integer type is converted to another integer type
other than _Bool,
if the new type is unsigned, the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type.



unsigned short us; / *假设USHRT_MAX == 65535 * /
us = -1000000;

该变量是无符号的,不能表示
负值。赋值必须将其设置为非负值,6.3.1.3描述如何计算该值:

-1000000
+65536(USHRT_MAX + 1)
========
-934464
+65536
========
-868928


========
-16960
+65536
========
48576(最终答案,瑞吉斯)
各种计算捷径可供选择;所有这些
添加物实际上都不需要实现以获得答案。

-
Eric Sosman
es ***** @ acm-dot-org.inva lid




考虑,

signed char sc = -4; / * binary = 11111100 * /

unsigned char uc = sc;


现在,如果我打印sc的值为252(二进制11111100)。


所以,如果你看不到转换已经完成。

" uc"中的位值与sc完全相同。


然后,是否需要执行如此多的添加/减少?



Consider,
signed char sc = -4; /* binary = 11111100 */
unsigned char uc = sc;

Now, if I print the value of sc it is 252 (binary 11111100).

So, if you see no conversion has been done. The bit values in
"uc" are exactly the same as in "sc".

Then, whats the need of performing so many additions/subtractions ?


考虑,
签名char sc = -4; / * binary = 11111100 * /
unsigned char uc = sc;

现在,如果我打印sc的值为252(二进制11111100)。

所以,如果你没有看到转换已经完成。
uc中的位值。与sc完全相同。
Consider,
signed char sc = -4; /* binary = 11111100 */
unsigned char uc = sc;

Now, if I print the value of sc it is 252 (binary 11111100).

So, if you see no conversion has been done. The bit values in
"uc" are exactly the same as in "sc".




位模式和实际值之间的混淆,这里。

首先,252 = -4 + 256,需要获得有效的无符号字符。

其次,并非所有机器都是2补码,但现在很多都是




正如Eric所说,有计算快捷方式,而且你需要注意它们依赖于机器(因此,在标准的

实现上) 。如果一个签名的字符和一个unsigned字符实际上与s的表示具有相同的

表示,那将是纯粹的巧合

和256 + s分别。



Great confusion between bit pattern and actual value, here.
First, 252 = -4 + 256, needed to get a valid unsigned char.
Second, not all machines are 2-complement, though many are
nowadays.

As Eric said, there are computational shortcuts, and you
should note they depend on the machine (hence, on the
implementation of the standard). It''s pure coincidence
if a signed char and an unsigned char have actually the same
representation for "s" and "256+s" respectively.


这篇关于有符号整数转换为无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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