2
byte op1 = 20;
sbyte op2 = 30;
var result = op1 & op2;

I know that & operator returns bool, but I cunfused now.

why equals result 20? Why is result Int32?

BurnOut
  • 111
  • 1
  • 7

3 Answers3

13

I know that & operator returns bool

That's only true when the operands are bool as well. The & operator is either logical or bitwise, depending on its operands. From the documentation:

Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

Perhaps you were thinking of the && operator, which is only a logical operator (on any of the predefined types), and which also performs short-circuiting.

As for why the result is int rather than byte, sbyte, short or ushort... C# doesn't define arithmetic/bitwise operators for types less than 32 bits. This is discussed in another Stack Overflow question, but it's not specific to &.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Seems odd that they'd make that distinction; for bool operands, isn't the logical AND the same as the bitwise AND? – Rawling Jul 25 '14 at 10:52
  • @Rawling: I try not to think of `bool` has having a particular bitwise representation at all. Imagine if you think of `true` as being "any value other than 0" for example... then you could have two `true` values of 1 and 2, which would have a bitwise AND of 0 (false), but a logical AND of true. – Jon Skeet Jul 25 '14 at 10:53
  • Huh. I thought that C# or the CLI fixed the `true & true == false ` problem by making a bool always all 1s under the hood, but after a bit more reading [that's apparently not the case](http://stackoverflow.com/questions/3335841/what-is-the-binary-representation-of-a-boolean-value-in-c-sharp) and the documentation *does* have to make this distinction. – Rawling Jul 25 '14 at 11:02
  • here is **[link](http://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html)** about **Bitwise operators** – BurnOut Jul 25 '14 at 11:03
  • Does not answer the question why the result is `Int32` and not some kind of `byte`. See my answer. – david.pfx Jul 25 '14 at 11:27
  • @david.pfx: Ah, missed that part - I was concentrating on "why isn't it bool". Will edit to indicate that, which is actually more fully explained in another SO question. – Jon Skeet Jul 25 '14 at 11:32
0

To my surprise, the other earlier answers missed the interesting part of this question, which sent me scrambling for the C# Language Specification.

Yes, the & operator is defined for a number of different types. And yes, if both arguments are bool then it performs a logical AND operation after evaluating both of its arguments. That is, no shortcircuit. The effect is to call the following function.

bool operator &(bool x, bool y);

But if the arguments are integers it performs a bitwise AND operation after evaluating both arguments and converting them to a common integral type. The effect is to call one of the following overloaded functions.

int operator &(int x, int y);
uint operator &(uint x, uint y);
long operator &(long x, long y);
ulong operator &(ulong x, ulong y);

Note that there is no short or byte versions of these functions, which is why the result in Int32.

I think this is the real answer to the question.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • This answers `Int32` vs `Byte` but the OP was expecing `Boolean`. – H H Jul 25 '14 at 11:41
  • I disagree, both counts. I think it answers the whole question, not just part. I think you can read OP as "I get why it might be bool or byte but why Int32?", but you'd have to ask him. – david.pfx Jul 25 '14 at 14:28
-1

The & is bitwise operator returns 1 if any one of two bits is 1 and it returns 0 if any of bits is zero. In case of integer or byte like in your problem it perform bitwise operation of AND between two operand so result will be an integer type not boolean. Try using && as it is logical operator.