开发者

Please explain output

#include<stdio.h>

int main(void) 
{
    int a=-3,b=5,c;开发者_如何学Python
    c=a|b;
    printf("%d ",c);
    c=a&b;
    printf("%d ",c);
}

The output is -3 5, please explain how?


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. Now just assume your ints are of length 8 bits (which is sufficient to examine 5 and -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

And now the bitwise AND:

1111 1101 & 0000 0101 = 0000 0101

Exactly the binary representation of 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 | or &.

When you OR those bits together, the 1s win. However, the 5 has a 0 in the same position as the 0 in -3, so that bit comes through the OR operation unchanged. The result (1111 1111 1111 1101) is still the same as -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. The result is still 5.


Binary of 5 --is--> 0000 0101

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 | 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

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)

hint: | means OR, & means 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

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


If you know all about 2's complements, then you should know

  1. how to write out 3 and 5 in 2's complement as bit31 bit32 ... bit3n and bit51 bit52 .. bit5n
  2. how to compute the result of bit3i | bit5i for i = 0 ... n
  3. how to convert the result back to base 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜