简体   繁体   English

一元运算符“&”如何工作

[英]How does Unary Operator '&' work

How does the unary operator '&' work ?一元运算符“&”如何工作?

In a test project i ran this code:在一个测试项目中,我运行了这段代码:

int num = 50, num2 = 100;

int x = num & num2;

result: x = 32结果:x = 32

 int num = 100, num2 = 90;

 int x = num & num2; 

result :x = 64结果:x = 64

How is this calculated ?这是如何计算的?

From MSDN :MSDN

Binary & operators are predefined for the integral types and bool.二进制和运算符是为整数类型和 bool 预定义的。 For integral types, & computes the logical bitwise AND of its operands.对于整数类型, & 计算其操作数的逻辑按位与。 For bool operands, & computes the logical AND of its operands;对于 bool 操作数,& 计算其操作数的逻辑与; that is, the result is true if and only if both its operands are true.也就是说,当且仅当它的两个操作数都为真时,结果才为真。

In your case it is integeral type version.在您的情况下,它是整数类型版本。

So:所以:

 50 in binary is 00110010     
100 in binary is 01100100     
AND result is    00100000 (32 dec)

Bitwise operator works on bits and perform bit-by-bit operation.按位运算符作用于位并执行逐位运算。

Binary AND Operator copies a bit to the result if it exists in both operands.如果两个操作数中都存在二进制 AND 运算符,则将其复制到结果中。

(A & B) = 12, i.e., 0000 1100

First thing: & in this context is a binary operator, as it has two arguments.第一件事: &在这种情况下是一个二元运算符,因为它有两个参数。 It is not a unary operator.不是元运算符。

& with two arguments is the bitwise AND operator.带有两个参数的&是按位 AND 运算符。

50 & 100 is 0b110010 & 0b1100100 . 50 & 1000b110010 & 0b1100100 Apply & to each bit gives you 0b100000 which is 32.&应用于每一位给你0b100000 ,即 32。

You can analyse 100 & 90 similarly.您可以类似地分析100 & 90

Binary values二进制值

50 (10 ) = 0110010  (2)
100 (10) = 1100100  (2)

And a logical AND is used so only the bits where both values are 1 are now one resulting in:并且使用逻辑与,因此只有两个值都为 1 的位现在为 1,从而导致:

0100000 (2) = 32 (10)

and

100 (10) = 1100100  (2)
AND
90 (10) =  1011010 (2)
------------------------
64 (10) =  1000000(2)

Represent both numbers as binaries (in order to see bits clearly when doing bitwise operations) and have a look at what's going on:将两个数字表示为二进制(以便在进行按位运算时清楚地看到)并查看发生了什么:

private static String AndExplanation(int left, int right) {
  String x = Convert.ToString(left, 2);
  String y = Convert.ToString(right, 2);
  String z = Convert.ToString(left & right, 2);

  int length = Math.Max(x.Length, y.Length);

  return String.Join(Environment.NewLine,
    $"{x.PadLeft(length, '0')} ({left}) &",
    $"{y.PadLeft(length, '0')} ({right})",
    new String('-', length),
    $"{z.PadLeft(length, '0')} ({left & right})"
  );
}

Tests测试

Console.Write(AndExplanation(50, 100));

Console.Write(AndExplanation(90, 100));

You'll see:你会看到的:

0110010 (50) &
1100100 (100)
-------
0100000 (32)

and

1011010 (90) &
1100100 (100)
-------
1000000 (64)

Short answer is - it depends.简短的回答是 - 这取决于。

For integers, as in your case it does bit operations.对于整数,就像在您的情况下一样,它执行位操作。 So 01100100 (100) bitwise AND 00110010 (50) = 00100000 (32).所以01100100 (100)按位 AND 00110010 (50) = 00100000 (32)。

For booleans that's an eager AND (evaluates both expressions always) - unlike the lazy AND && which doesn't evaluate the second expression if first is false .对于急切 AND 的布尔值(始终评估两个表达式) - 与惰性 AND &&不同,如果 first 为false则不评估第二个表达式。

This is a bitwise AND:这是一个按位与:

In "0b0110010 AND 0b1100100", you should look for the the 1's and 0's shared at common places, which gives you 0b0100000=32.在“0b0110010 AND 0b1100100”中,您应该查找在公共位置共享的 1 和 0,这为您提供 0b0100000=32。

The same holds for "100 AND 90" which gives you 0b1000000=64.这同样适用于“100 AND 90”,它给你 0b1000000=64。

I hope it helped.我希望它有所帮助。

It's a bits operator and .它是一个位运算符 The bits(binary) for 50 is 110010 and 100 is 1100100, when you align them (flush to the right), you get 110010 1100100 50 的位(二进制)是 110010 和 100 是 1100100,当你对齐它们(向右对齐)时,你得到110010 1100100

The & operator works in such a way that, at each position, you get 1 if both lines have 1 and you get 0 otherwise, as shown below. & 运算符的工作方式是,在每个位置,如果两行都为 1,则为 1,否则为 0,如下所示。 110010 1100100 ||||||| 0100000 110010 1100100 ||||||| 0100000 So you get the binary result 100000, which is 32. 110010 1100100 ||||||| 0100000所以你得到二进制结果 100000,也就是 32。

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

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