简体   繁体   English

请说明输出

[英]Please explain output

#include<stdio.h>

int main(void) 
{
    int a=-3,b=5,c;
    c=a|b;
    printf("%d ",c);
    c=a&b;
    printf("%d ",c);
}

The output is -3 5 , please explain how? 输出为-3 5 ,请解释如何?

To understand the output, you need to become familiar with the Two's Complement which is used to represent negative binary numbers. 要了解输出,您需要熟悉用于表示负二进制数的二进制码。 The conversion from +x to -x is actually quite easy: Complement all bits and add one. 从+ x到-x的转换实际上非常容易:对所有位进行补码并加一位。 Now just assume your ints are of length 8 bits (which is sufficient to examine 5 and -3): 现在,假设您的int的长度为8位(足以检查5和-3):

5: 0000 0101
3: 0000 0011 => -3: 1111 1101

Now lets take a look at the bitwise or: 现在让我们看一下按位或:

1111 1101 | 0000 0101 = 1111 1101

Exactly the represantation of -3 正好代表-3

And now the bitwise AND: 现在按位与:

1111 1101 & 0000 0101 = 0000 0101

Exactly the binary representation of 5 完全是5的二进制表示形式

It helps when you look at the binary representations alongside each other: 当您同时查看二进制表示形式时,它会有所帮助:

  • -3 == 1111 1111 1111 1101
  • +5 == 0000 0000 0000 0101

The thing to understand is that both | 需要明白的一点是,无论| and & will leave a bit alone if it has the same value on both sides. 如果两边的值相同,则&将单独保留。 If the values are different (ie one operand has a 0 at that position and the other has a 1), then one of them "wins", depending on whether you're using | 如果值不同(即,一个操作数在该位置为0,另一个操作数为1),则其中一个“获胜”,这取决于您是否使用| or & . &

When you OR those bits together, the 1s win. 当您将这些位或或在一起时,将赢得1。 However, the 5 has a 0 in the same position as the 0 in -3, so that bit comes through the OR operation unchanged. 但是,5在与-3中的0相同的位置具有0,因此该位通过OR操作不变。 The result ( 1111 1111 1111 1101 ) is still the same as -3. 结果( 1111 1111 1111 1101 )仍与-3相同。

When you do a bitwise AND, the zeroes win. 当您进行按位与运算时,零将获胜。 However, the 1s in 5 match up with 1s in -3, so those bits come through the AND operation unchanged. 但是,5中的1与-3中的1匹配,因此那些位不变地通过AND操作获得。 The result is still 5. 结果仍然是5。

Binary of 5 --is--> 0000 0101 5二进制数-是-> 0000 0101

3 --> 0000 0011 -- 1's Complement --> 1111 1100 -- 2's Complement (add 1) --> 1111 1101 == -3. 3 --> 0000 0011 1's Complement -> 1111 1100 2's Complement (add 1) -> 1111 1101 == -3。 This is how it gets stored in Memory. 这是将其存储在内存中的方式。

Bitwise OR Truth Table: 按位或真值表:

           p OR q

p     ||    q      ||   p | q
T(1)  ||    T(1)   ||     T(1)
T(1)  ||    F(0)   ||     T(1)
F(0)  ||    T(1)   ||     T(1)
F(0)  ||    F(0)   ||     F(0)

1111 1101 | 1111 1101 | 0000 0101 = 1111 1101 == -3 0000 0101 = 1111 1101 == -3

Bitwise AND Truth Table: 按位与真值表:

          p AND q 

p     ||    q      ||   p & q
T(1)  ||    T(1)   ||     T(1)
T(1)  ||    F(0)   ||     F(0)
F(0)  ||    T(1)   ||     F(0)
F(0)  ||    F(0)   ||     F(0)

1111 1101 & 0000 0101 = 0000 0101 == 5 1111 11010000 0101 = 0000 0101 == 5

Also, see - What Every Computer Scientist Should Know About Floating-Point Arithmetic . 另请参阅- 每位计算机科学家都应了解的浮点运算法则

Get some paper and a writing implement of your choice 获得一些论文和您选择的书写工具

Write out -3 and 5 in binary ( See twos complement for how to do negative numbers) 用二进制写出-3和5(有关如何做负数,请参见二进制补码)

hint: | 提示: means OR, & means AND 表示OR,&表示AND

-3 = 0xFFFD = 1111 1111 1111 1101 5 = 0101 so the bitwise or does not change the first argument ( just overlap one with ones ) and results is still -3 -3 = 0xFFFD = 1111 1111 1111 1101 5 = 0101,因此按位或不更改第一个参数(只是将一个与一个参数重叠),结果仍然为-3

The bitwise and takes the coomon ones between 1101 and 0101 that is 0101=5 :) no reason to consider all the trailing one in -3 since 5 = 0000 0000 0000 0101 按位取1101和0101之间的coomon值,即0101 = 5 :)由于5 = 0000 0000 0000 0101,没有理由考虑-3中的所有尾随位

If you know all about 2's complements, then you should know 如果您完全了解2的补数,那么您应该知道

  1. how to write out 3 and 5 in 2's complement as bit31 bit32 ... bit3n and bit51 bit52 .. bit5n 如何写出2的补码中的3和5作为bit31 bit32 ... bit3n和bit51 bit52 .. bit5n
  2. how to compute the result of bit3i | 如何计算bit3i的结果| bit5i for i = 0 ... n i = 0 ... n的bit5i
  3. how to convert the result back to base 10 如何将结果转换回以10为底

That should give you your answer for the first, do the same with & for the second. 那应该给您第一个答案,第二个与&相同。

Ever hear of DeMorgan 's law...??? 有没有听说过德摩根的法律……? The hint is in the linky , it is the table that epitomises and embodies the raw cold truth of logic that is pinned into the syntaxes of major language compilers... 提示在链接中 ,它是表格的缩影,体现了逻辑的原始事实,这些事实被钉在主要语言编译器的语法中...

Even more worrying is the fact that you don't have the basic CS101 knowledge and posting this question (Sorry if you take this to be condescending but am not), I genuinely cannot believe you're looking at a C code and not told anything about two's complements, bitwise logic... something is very wrong here... If your college lecturer has not told you any of it, said lecturer should not be lecturing at all and find another job.... sigh 更令人担忧的是,您不具备CS101的基本知识并发布了此问题(很抱歉,如果您认为这是屈服的,但不是),我真的不敢相信您正在看C代码并且什么都没告诉关于二进制补码,按位逻辑...什么是大错......如果你的讲师团还没有告诉你它的任何表示,讲师不应该在所有的演讲另谋高就.... 叹息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM