简体   繁体   English

为什么&运算符返回Int32而不是bool?

[英]why & operator returns Int32 instead of bool?

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

I know that & operator returns bool, but I cunfused now. 我知道&运营商返回bool,但我现在已经收费了。

why equals result 20? 为什么等于结果20? Why is result Int32? 为什么结果是Int32?

I know that & operator returns bool 我知道&运营商返回bool

That's only true when the operands are bool as well. 当操作数也是bool时,这才是真的。 The & operator is either logical or bitwise, depending on its operands. &运算符是逻辑或按位,具体取决于其操作数。 From the documentation : 文档

Binary & operators are predefined for the integral types and bool. 二进制和运算符是为整数类型和bool预定义的。 For integral types, & computes the logical bitwise AND of its operands. 对于整数类型,&计算其操作数的逻辑按位AND。 For bool operands, & computes the logical AND of its operands; 对于bool操作数,&计算其操作数的逻辑AND; 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. 至于为什么结果是int而不是bytesbyteshortushort ... C#没有为小于32位的类型定义算术/位运算符。 This is discussed in another Stack Overflow question , but it's not specific to & . 这在另一个Stack Overflow问题中讨论过,但它并不特定于&

To my surprise, the other earlier answers missed the interesting part of this question, which sent me scrambling for the C# Language Specification. 令我惊讶的是,其他早期的答案错过了这个问题的有趣部分,这让我忙于争取C#语言规范。

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. 是的,如果两个参数都是bool那么它在评估它们的两个参数后执行逻辑AND操作。 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 . 但是如果参数是整数,它在评估两个参数并将它们转换为公共整数类型之后执行按位AND运算。 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 . 请注意,这些函数没有short版本或byte版本,这就是Int32的结果。

I think this is the real answer to the question. 我认为这是问题的真正答案。

The & is bitwise operator returns 1 if any one of two bits is 1 and it returns 0 if any of bits is zero. 如果两位中的任何一位为1,则&是按位运算符返回1,如果任何位为零,则返回0。 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. 在你的问题中像整数或字节的情况下,它在两个操作数之间执行AND的按位运算,因此结果将是整数类型而不是布尔值。 Try using && as it is logical operator. 尝试使用&&,因为它是逻辑运算符。

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

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