简体   繁体   English

我该如何解决这种 python 表达式?

[英]How do I can solve this kind of python expression?

I have two expressions like given below:我有两个表达式,如下所示:

a,b,c=2,4,5

print(a|b*c)
output : 22

How it is returning me the 22

and second expression is:第二个表达式是:

print(a|b^c)

output : 3

Can anyone please explain to me how python calculating this?谁能向我解释一下 python 是如何计算这个的? I am a newbie in python I don't know how it is calculating this.我是 python 的新手,我不知道它是如何计算的。

EXPRESSION # 1表达#1

Lets consider,让我们考虑一下,

a|b*c

Precedence of * (multiplication) is greater than | * (乘法)的Precedence大于| (bitwise OR) thats why first b*c will be computed and then its output will be computed with a (按位或)这就是为什么第一个b*c将被计算,然后它的 output 将被计算a

STEPS:脚步:

1- b*c will return 20. 1- b*c将返回 20。

2- Then a will perform bit wise OR operation with b*c . 2- 然后a将与b*c执行按位或运算。 SeeBitwise OR operation ( OR returns 1 when there is any one else 0)请参见Bitwise OR operation (当有任何一个时 OR 返回 1,否则为 0)

3- Convert values of a and b*c to binary and perform bitwise operation. 3- 将ab*c的值转换为二进制并执行按位运算。 SeeConvert decimal to binary请参阅Convert decimal to binary

      1 0     > a = 2
1 0 1 0 0     > b*c = 20
_________
1 0 1 1 0     > a|b*c = 22

EXPRESSION # 2表达#2

Lets consider,让我们考虑一下,

a|b^c

Precedence of ^ (bitwise XOR) is greater than | ^ (按位异或)的Precedence大于| (bitwise OR) thats why first b^c will be computed and then its output will be computed with a (按位或)这就是为什么第一个b^c将被计算,然后它的 output 将被计算a

STEPS:脚步:

1- Bitwise XOR operation will be performed between b and c (b^c). 1- 将在 b 和 c (b^c) 之间执行按位异或运算。 SeeBitwise XOR operation ( XOR returns 1 when there are odd number of ones else 0)请参见Bitwise XOR operation (当有奇数个时,异或返回 1,否则为 0)

1 0 0   > b = 4
1 0 1   > c = 5
______   
0 0 1   > b^c = 1

2- Then a will perform bit wise OR operation with b^c . 2- 然后a将与b^c执行按位或运算。 SeeBitwise OR operation ( OR returns 1 when there is any one else 0)请参见Bitwise OR operation (当有任何一个时 OR 返回 1,否则为 0)

1 0   > a =2
0 1   > b^c = 1
___
1 1   > a|b^c = 3

print(a|b*c)打印(a|b*c)

Because of the operator precedence , the multiplication ( * ) will be processed before the bitwise OR ( | ), so the operations will happen in the following order:由于运算符优先级,乘法( * )将在按位或( | )之前处理,因此操作将按以下顺序进行:

  1. b*c = 4*5 = 20
  2. a|b*c = 2|20 , ie 10 OR 10100 = 10110 in binary a|b*c = 2|20 ,即10 OR 10100 = 10110二进制

That gives us a final answer of 22 .这给了我们22的最终答案。

print(a|b^c)打印(a|b^c)

Likewise, due to the operator precedence , the bitwise XOR ( ˆ ) will be processed first, so we'll have:同样,由于运算符优先级,按位异或 (^ ˆ将首先处理,因此我们将有:

  1. bˆc = 4ˆ5 , ie 100 XOR 101 = 001 in binary bˆc = 4ˆ5 ,即二进制的100 XOR 101 = 001
  2. a|bˆc = 2|1 , ie 10 OR 01 = 11 in binary a|bˆc = 2|1 ,即二进制的10 OR 01 = 11

That gives us a final answer of 3 .这给了我们一个3的最终答案。

This is doing a mix of bitwise and integer arithmetic operations.这是按位和 integer 算术运算的混合。

How does that work?这是如何运作的?

Based on the order of precedence of operators defined in the docs , the first expression to be evaluated is b*c, which returns 20. Then we evaluate the expression a|20.根据docs中定义的运算符的优先顺序,要计算的第一个表达式是 b*c,它返回 20。然后我们计算表达式 a|20。 This is a bitwise operation on the two integers, so what we are doing is doing a logical OR on each bit of the binary representation of these two numbers ie (00010) |这是对两个整数的按位运算,所以我们正在做的是对这两个数字的二进制表示的每一位进行逻辑或,即 (00010) | (10100) which gives us (10110) ie 22 (10100) 这给了我们 (10110) 即 22

Similarly, in the second example, we first do XOR on b and c, which is equal to 1 (calculated as binary 100 XOR 101 = 001).同样,在第二个例子中,我们首先对 b 和 c 进行 XOR,等于 1(计算为二进制 100 XOR 101 = 001)。 The result is then bitwise ORed with a, returning the value 3然后将结果与 a 进行按位或运算,返回值 3

The Precedence of arithmetic operators * , /, +, - is more than bitwise operators &, |, ^ and both types of operators are evaluated left to right if the same precedence is found.算术运算符 * , /, +, -的优先级大于位运算符&, |, ^并且如果找到相同的优先级,则从左到右计算两种类型的运算符。

Moreover,而且,

  • for arithmetic operator precedence order is: * ,/ then +,-算术运算符的优先顺序是: * ,/ 然后 +,-
  • and for bitwise operator precedence order is: &, ^, |对于按位运算符,优先顺序为: &、^、|

Check this article for more information查看这篇文章了解更多信息

Coming to the solution:来到解决方案:

  1. The first expression evaluates like this: a|(b*c)第一个表达式的计算结果如下: a|(b*c)
  2. The second expression evaluates like this: a|(b^c)第二个表达式的计算结果如下:a|(b^c)

You need to break down the numbers into their binary representation to see what's happening under the hood.您需要将这些数字分解为它们的二进制表示,以了解幕后发生的事情。

Case 1,情况1,

Binary for a (2) - 00010 (2) 的二进制 - 00010

Binary for b*c (20) - 10100 b*c 的二进制 (20) - 10100

So if you do a bitwise OR (|) you'll get a binary sum of above two ie 10110 = 22因此,如果您执行按位或 (|),您将得到上述两个的二进制和,即 10110 = 22

Case 2,案例2,

Binary for a (2) - 00010 (2) 的二进制 - 00010

Binary for b (4) - 00100 b (4) 的二进制 - 00100

Binary for c (5) - 00101 c (5) - 00101 的二进制文件

Again if you do a bitwise XOR (^) you'll get a binary exclusive or of b and c ie 00001= 1同样,如果您执行按位异或 (^),您将得到 b 和 c 的二进制异或,即 00001= 1

Finally if you do a bitwise OR (|) you'll get a binary sum of a with b^c ie 00011 = 3最后,如果您执行按位 OR (|),您将得到 a 与 b^c 的二进制和,即 00011 = 3

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

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