C # Bitwise operator with ints

What does this expression mean?

Note. x and y vars are just the sample values.

int x = 3; int y = 1; if ((x & y) !=0) 

I inherited the code base and can't get to the speed with bitwise operators. I read, but still something is missing. Help!

0
source share
3 answers

This is a comparison of the bits in each value. It returns any bits that are set in both numbers.

In your example:

  3: 0011 1: 0001 3 & 1: 0001 
+8
source

This checks if x and y both have at least one common bit. In the case of your example, this will be true.

+2
source
 if ((x & y) != 0) 

This is usually used to determine if x specific bit flag ( y ). The AND operator returns an integer with only those bits that are set on both operands.

+1
source

All Articles