简体   繁体   English

三元运算符是什么意思

[英]What does & mean as a ternary operator

I have this piece of code I'm going to translate into mips but I don't know what it means: A = B & C[0] 我有这段代码要翻译成mips,但我不知道这是什么意思: A = B & C[0]

When I google ternary operator I can't find any mention of the & , just e1 ? e2 : e 当我使用google三元运算符时,找不到&任何提示,只是e1 ? e2 : e e1 ? e2 : e

What does it mean? 这是什么意思?

There's no ternary operator here. 这里没有三元运算符。 The expression is equivalent to A = (B & C[0]) , that is A is assigned with the result of bitwise AND applied to B and C[0] . 该表达式等效于A = (B & C[0]) ,即将A分配给BC[0]的按位与结果。

The & here is the binary (2 inputs) operator "bitwise and". &此处是二进制(2个输入)运算符“按位与”。 In this case you have: 在这种情况下,您可以:

A = B & C[0];

So A will receive the results of the bit-by-bit and-ing of B and C[0] . 因此, A将接收BC[0]的逐位和运算结果。 Lets imagine that A, B and C[0] are two 32 bit integers, that have values I randomly assigned them, and lets view this operation in binary: 假设A,B和C [0]是两个32位整数,它们的值是我随机分配的,然后以二进制形式查看此操作:

B    = 0000 0001 0010 0100 1000 1001 1011 1111
C[0] = 1001 1110 1101 1101 1010 1010 1010 0101
A    = 0000 0000 0000 0100 1000 1000 1010 0101 (the and of the two values above)

that is, A has a 1 bit only where both B and C[0] have 1 bits. 也就是说, A具有1位只有其中两个BC[0]1比特。

In decimal this is 19171775 & 2665327269 = 297125 . 以十进制表示的是19171775 & 2665327269 = 297125

BTW, a ternary operator has 3 inputs, most common being ?: , as in R = C ? B : A 顺便说一句,三元运算符有3个输入,最常见的是?: ,如R = C ? B : A R = C ? B : A . R = C ? B : A

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

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