本文介绍了一个人的赞美运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






当我执行以下程序时,找到'赞成2',

我得到-3在哪里

我期待-2。我的

理解中有什么根本的错误吗?

main()

{

int a = 2;


printf("%d \ n",〜a);

}


O / P:

-3


谢谢,

迪帕克

解决方案



对于2'的补码机,输出是正确的。



将该位模式转换为一个补码机器,并且它将在那里打印-2。两个补码

机器上的相同位模式为-3。



一个补码比两个补码少一个。

在二进制补码机上(接近所有的机器,

特别是现代的机器),N的两个补码显示为-N的n /和
,以及N的一个补码是-N-1。


例如,两个'的0的补码是0,而

的补码是0 -1。




输出正确,两个'补机。



将该位模式转换为一个补码机,并且它将打印-2。两个补码

机器上的相同位模式为-3。



一个补码比两个补码少一个。

在二进制补码机上(接近所有的机器,

特别是现代的机器),N的两个补码显示为-N的n /和
,以及N的一个补码是-N-1。





1'改为2'的补码将获得什么机器补充?




是的,有。



仔细检查以下代码的输出,并试着看看是什么? br />
正在发生。请注意,下面的代码与您的代码不同,正确指定了包含标题的
,正确指示main的返回类型,并且

正确返回main的值(以及定义的意思)。


#include< stdio.h>

#include< string.h>


int main(无效)

{

/ *为了清楚起见,下面有目的效率低下* /

unsigned int x ;

签署了y,y2;

for(y = 10; y> = -10; y--){

memcpy (& x,& y,sizeof x);

printf(" y =%3d,x =%#0o |",y,x);

x = ~x;

memcpy(& y2,& x,sizeof x);

printf(" ~x =%#0o,y2 =% d \ n",x,y2);

}

返回0;

}

y = 10 ,x = 012 | ~x = 037777777765,y2 = -11

y = 9,x = 011 | ~x = 037777777766,y2 = -10

y = 8,x = 010 | ~x = 037777777767,y2 = -9

y = 7,x = 07 | ~x = 037777777770,y2 = -8

y = 6,x = 06 | ~x = 037777777771,y2 = -7

y = 5,x = 05 | ~x = 037777777772,y2 = -6

y = 4,x = 04 | ~x = 037777777773,y2 = -5

y = 3,x = 03 | ~x = 037777777774,y2 = -4

y = 2,x = 02 | ~x = 037777777775,y2 = -3

y = 1,x = 01 | ~x = 037777777776,y2 = -2

y = 0,x = 0 | ~x = 037777777777,y2 = -1

y = -1,x = 037777777777 | ~x = 0,y2 = 0

y = -2,x = 037777777776 | ~x = 01,y2 = 1

y = -3,x = 037777777775 | ~x = 02,y2 = 2

y = -4,x = 037777777774 | ~x = 03,y2 = 3

y = -5,x = 037777777773 | ~x = 04,y2 = 4

y = -6,x = 037777777772 | ~x = 05,y2 = 5

y = -7,x = 037777777771 | ~x = 06,y2 = 6

y = -8,x = 037777777770 | ~x = 07,y2 = 7

y = -9,x = 037777777767 | ~x = 010,y2 = 8

y = -10,x = 037777777766 | ~x = 011,y2 = 9



Hi,

when I execute following program which find''s one''s compliment of 2,
I''m getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3

Thanks,
Deepak

解决方案

That output is correct, for a two''s complement machine.

Take that bit pattern over to a one''s complement machine, and it
will print -2 there. The same bit pattern on a two''s complement
machine is -3.

The one''s complement is one less than the two''s complement.
On a two''s complement machine (close to all machines are,
especially modern ones), the two''s complement of N is
displayed as -N, and the one''s complement of N is -N-1.

For example, the two''s complement of 0 is 0, and the
one''s complement of 0 is -1.



That output is correct, for a two''s complement machine.


Take that bit pattern over to a one''s complement machine, and it
will print -2 there. The same bit pattern on a two''s complement
machine is -3.


The one''s complement is one less than the two''s complement.
On a two''s complement machine (close to all machines are,
especially modern ones), the two''s complement of N is
displayed as -N, and the one''s complement of N is -N-1.

What machine is going to gain by changing it to 2''s complement from
1''s complement?


Yes, there is.

Examine closely the output of the following code and try to see what''s
happening. Note that the code below, unlike yours, properly specifies
the headers included, properly indicates the return type for main, and
properly returns a value from main (and one with a defined meaning).

#include <stdio.h>
#include <string.h>

int main(void)
{
/* There are purposeful inefficiencies below for the sake of clarity */
unsigned int x;
signed int y, y2;
for (y = 10; y >= -10; y--) {
memcpy(&x, &y, sizeof x);
printf("y = %3d, x = %#0o | ", y, x);
x = ~x;
memcpy(&y2, &x, sizeof x);
printf("~x = %#0o, y2 = %d\n", x, y2);
}
return 0;
}
y = 10, x = 012 | ~x = 037777777765, y2 = -11
y = 9, x = 011 | ~x = 037777777766, y2 = -10
y = 8, x = 010 | ~x = 037777777767, y2 = -9
y = 7, x = 07 | ~x = 037777777770, y2 = -8
y = 6, x = 06 | ~x = 037777777771, y2 = -7
y = 5, x = 05 | ~x = 037777777772, y2 = -6
y = 4, x = 04 | ~x = 037777777773, y2 = -5
y = 3, x = 03 | ~x = 037777777774, y2 = -4
y = 2, x = 02 | ~x = 037777777775, y2 = -3
y = 1, x = 01 | ~x = 037777777776, y2 = -2
y = 0, x = 0 | ~x = 037777777777, y2 = -1
y = -1, x = 037777777777 | ~x = 0, y2 = 0
y = -2, x = 037777777776 | ~x = 01, y2 = 1
y = -3, x = 037777777775 | ~x = 02, y2 = 2
y = -4, x = 037777777774 | ~x = 03, y2 = 3
y = -5, x = 037777777773 | ~x = 04, y2 = 4
y = -6, x = 037777777772 | ~x = 05, y2 = 5
y = -7, x = 037777777771 | ~x = 06, y2 = 6
y = -8, x = 037777777770 | ~x = 07, y2 = 7
y = -9, x = 037777777767 | ~x = 010, y2 = 8
y = -10, x = 037777777766 | ~x = 011, y2 = 9


这篇关于一个人的赞美运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:57