按位补码 [英] bitwise complement

查看:163
本文介绍了按位补码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:


#include< stdio.h>


int main(无效)

{

unsigned char c = 0;


unsigned int i = ~c;


printf( i =%u \ n,i);


返回0;


}


我使用32位VC ++和32位gcc编译并运行它。


在这两种情况下,此代码打印4294967295(即2 ^ 32 - 1) -

UINT_MAX。


我的疑问:既然我们正在服用~c,不应该分配给我。

Consider the following code:

#include<stdio.h>

int main(void)
{
unsigned char c = 0;

unsigned int i = ~c;

printf("i = %u\n", i);

return 0;

}

I compiled and ran this with 32-bit VC++ and 32-bit gcc.

In both cases, this code prints 4294967295(which is 2^32 - 1) -
UINT_MAX.

My doubt: since we are taking ~c, should''t 255 be assigned to i.

推荐答案

2006-12-20< 11 ********************** @ 79g2000cws。 Google网上论坛。 com>,

subramanian写道:
2006-12-20 <11**********************@79g2000cws.googlegroups. com>,
subramanian wrote:

请考虑以下代码:


#include< stdio.h>


int main(无效)

{

unsigned char c = 0;

unsigned int i = ~c;

printf(" i =%u \ n",i);

返回0;
}


我使用32位VC ++和32位gcc编译并运行它。


在这两种情况下,此代码打印4294967295(这是2 ^ 32 - 1) -

UINT_MAX。


我怀疑:因为我们正在服用~c,所以不应该分配给我。
Consider the following code:

#include<stdio.h>

int main(void)
{
unsigned char c = 0;
unsigned int i = ~c;
printf("i = %u\n", i);
return 0;
}

I compiled and ran this with 32-bit VC++ and 32-bit gcc.

In both cases, this code prints 4294967295(which is 2^32 - 1) -
UINT_MAX.

My doubt: since we are taking ~c, should''t 255 be assigned to i.



只要在表达式中使用char或short类型的值,它就会以无效的方式转换为int。这很荒谬,但需要

标准。


所以,当你使用c时,它变为0作为int,那么~c是未指定的

(实现定义?)但显然被你的

实现解释为-1,然后当分配给unsigned int时,-1变为

UINT_MAX。

Whenever a value of char or short type is used in an expression, it
is silently converted to int. This is ridiculous, but required by the
standard.

So, when you use c, it becomes 0 as an int, then ~c is unspecified
(implementation-defined?) but apparently interpreted by your
implementation as -1, then when assigning to an unsigned int, -1 becomes
UINT_MAX.




" subramanian" < su ************** @ yahoo.comwrote in message

news:11 **************** ******@79g2000cws.googlegro ups.com ...

"subramanian" <su**************@yahoo.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...

请考虑以下代码:


#包括< stdio.h>


int main(无效)

{

unsigned char c = 0;


unsigned int i = ~c;


printf(" i =%u \ n",i);

返回0;


}


我使用32位VC ++和32位gcc编译并运行它。


在这两种情况下,此代码打印4294967295(即2 ^ 32 - 1) -

UINT_MAX。


我的疑问:因为我们正在服用~c,所以不应该将255分配给我。
Consider the following code:

#include<stdio.h>

int main(void)
{
unsigned char c = 0;

unsigned int i = ~c;

printf("i = %u\n", i);

return 0;

}

I compiled and ran this with 32-bit VC++ and 32-bit gcc.

In both cases, this code prints 4294967295(which is 2^32 - 1) -
UINT_MAX.

My doubt: since we are taking ~c, should''t 255 be assigned to i.



"〜运算符的结果是按位的补码它(推广)

操作数(即

结果中的每一位都被设置当且仅当

中的相应位被转换操作数是

未设置)。整数提升在操作数上执行,而

结果具有

提升类型。如果提升类型是无符号类型,则表达式~E

相当于

到该类型中可表示的最大值减去E.

"The result of the ~ operator is the bitwise complement of its (promoted)
operand (that is,
each bit in the result is set if and only if the corresponding bit in the
converted operand is
not set). The integer promotions are performed on the operand, and the
result has the
promoted type. If the promoted type is an unsigned type, the expression ~E
is equivalent
to the maximum value representable in that type minus E."


Random832< ra **** @ random.yi.orgwrites:
Random832 <ra****@random.yi.orgwrites:

每当使用char或short类型的值时在表达式中,它将
静默转换为int。这很荒谬,但需要

标准。
Whenever a value of char or short type is used in an expression, it
is silently converted to int. This is ridiculous, but required by the
standard.



准确的规则是:


如果int可以代表原始类型的所有值,

值转换为int;否则,它被转换为

一个unsigned int。


因为char可能与unsigned int具有相同的范围,所以

表示可以将char提升为unsigned int而不是

int。 (但这肯定是一个不寻常的案例。)

-

我在我的DeathStation 9000上运行它,恶魔飞出了我的鼻子。 --Kaz

The precise rule is:

If an int can represent all values of the original type, the
value is converted to an int; otherwise, it is converted to
an unsigned int.

Because a char might have the same range as unsigned int, this
means that a char could be promoted to unsigned int instead of to
int. (But that''s certainly an unusual case.)
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz


这篇关于按位补码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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