简体   繁体   中英

The c program results in output 2 why?

#include <stdio.h>
int main()
{
    int x = 0, y = 2;
    int z = ~x & y;
    printf("%d\n", z);
}

can any body tell the how the operation is being take place with respect to how the variables are saved in the memory

Let me make this shorter using just 8 bits:

x         0   00000000
y         2   00000010
~x       -1   11111111
~x & y    2   00000010

Bitwise-complement ~ will complement (invert value) of each bit of its operand ( 1 becomes 0 and 0 becomes 1 ). Bitwise-AND & will set a bit to 1 if it's 1 in both its operands:

lhs    rhs    lhs AND rhs
 0      0          0   
 0      1          0   
 1      0          0   
 1      1          1

Then, for example, 011b & 001b will result in 001b (because only LSB is 1 in both operands). In your case negating x (which is 0 ) you have 32 bits set to 1 so result will depend entirely by y (because 1 AND RHS = RHS , see last two lines in the truth table).

Important: Please note that your code isn't portable, according to ANSI C bitwise-AND behavior for signed integers is implementation defined (so what works in your implementation may be broken on another platform/compiler or with another compiler version).

I'll use binairy to explain why

int x = 0;      //x = 0000
int y = 2;      //y = 0010
int z = ~x & y;

Now, ~x is x inverse so 1111 The & does a bitwize and ( && does logical and) so:

~x & y
1111 & 0010
0010

And 0010 is 2

Note: I'm using 4 bit's but in fact depending on implementation this could be 32 bit (or 16). The idea remains the same

Understanding the operator ~ is the key, and note that ~ operator is not the same as the NOT operator.

x = 0

~x = 0xFFFFFFFF

y = 0x00000002;

z = 0xFFFFFFFF & 0x00000002;

z = 0x2;

The Bitwise Complement

The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.

To know more about bitwise operators visit this tutorial .

For the sake of the example I'd use unsigned integers so we will calculate the binary way more easily.

If integer x is 0 than its value is 00000000000000000000000000000000 (32 bits - each one is 0).

Now, when you say ~x you mean complement x which flips all of the bits - each bit that was on would be now off and each bit that was off would be on. Means X would be now 11111111111111111111111111111111 (32 bits - each one is 1).

It is the maximum value an unsigned integer can contain. When you use bitwise AND on all of the bits of a number which is ONLY ones, the result would be the second number. Examples:

1 & 0 is 0.
11 & 01 is 01.
111 & 001 is 001
111111111111 & 00100101 is 00100101

And so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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