简体   繁体   English

C:数字前面的符号

[英]C: Ampersand in front of a number

What does this line (x = n & 5;) mean in the code below?在下面的代码中,这一行 (x = n & 5;) 是什么意思? As far as I know ampersand is used for pointers.据我所知,&符号用于指针。 I was not expecting this code to compiled, but it compiled and ran fine.我没想到这段代码会编译,但它编译并运行良好。 The results I got is我得到的结果是

0,1,0,1,4,5,4,5,0,1, 0,1,0,1,4,5,4,5,0,1,

#include <stdio.h>

int main(void){
    int x, n;
    for (n = 0; n < 10; n++){
        x = n & 5;
        printf("%d,", x);
    }
    printf("\n");
    return 0;
}

In this case it's bitwise AND.在这种情况下,它是按位与。

x = n & 5;

will AND 5 (which is 0b101) with whatever is in n . will AND 5(即 0b101)与n中的任何内容。

AND works on the bits that represent the values. AND 作用于代表值的位。 The result will have a 1 if both values have a 1 in that position, 0 otherwise.如果 position 中的两个值均为 1,则结果将为 1,否则为 0。

Since we're ANDing with 5, and there are only 4 values you can make with the two bits in 0b101, the only possible values for x are 1, 4, 5 and 0. Here's an illustration:由于我们与 5 进行与运算,而 0b101 中的两位只能产生 4 个值,因此 x 的唯一可能值是 1、4、5 和 0。这是一个示例:

n           &    5     = x
1 (0b0001)  &  0b0101  = 1  (0b0001)
2 (0b0010)  &  0b0101  = 0  (0b0000)
3 (0b0011)  &  0b0101  = 1  (0b0001)
4 (0b0100)  &  0b0101  = 4  (0b0100)
5 (0b0101)  &  0b0101  = 5  (0b0101)
6 (0b0110)  &  0b0101  = 4  (0b0100)
7 (0b0111)  &  0b0101  = 4  (0b0100)
8 (0b1000)  &  0b0101  = 0  (0b0000)
9 (0b1001)  &  0b0101  = 1  (0b0001)

That's the "bitwise and" operator.那就是“按位与”运算符。 Normally, it takes two integers, and returns an integer that has only the bits set that are in both of it's parameters.通常,它需要两个整数,并返回一个 integer,它只设置了两个参数中的位。

base10 base2
6       0110
3       0011
6&3     0010   (=2)

There's also "bitwise or" |还有“按位或” | which sets bits that either one has set.它设置其中任何一个已设置的位。

base10 base2
6       0110
3       0011
6|3     0111   (=7)

and there's "bitwise xor" ^ which sets bits that are different.还有“按位异或” ^设置不同的位。

base10 base2
6       0110
3       0011
6^3     0101   (=5)

It's a bitwise AND operator.它是按位 AND 运算符。 The value of n is being ANDed with 5. n 的值与 5 进行“与”运算。

You were already told that in this case it's a binary and.你已经被告知在这种情况下它是一个二进制和。 But this requires a bit more explanation.但这需要更多的解释。 In C and other C-like languages there are two & operators.在 C 和其他类 C 语言中,有两个&运算符。 One is unary the other binary .一个是一元的,另一个是二进制的。

A unary operator acts on a single value alone, while a binary operator takes in two values.元运算符单独作用于单个值,而二元运算符接受两个值。 In C binary operators take precedence over unary ones.在 C 中,二元运算符优先于一元运算符。 If you write如果你写

int b;
int *a = &b;

Then b is the only value the operator can work on.那么b是操作员可以处理的唯一值。 If you write however但是如果你写

int c, d;
int d = c & d;

then the operator has two values to work with and the binary interpretation takes precedence over the unary interpretation.那么运算符有两个值可以使用,二元解释优先于一元解释。 Note that this does not only apply to the & operator, but also its counterpart *请注意,这不仅适用于&运算符,还适用于它的对应物*

int *f;
int h = *f;

But also但是也

int i,j;
int k = i * j;

Like with all operators, precedence can be overridden with parentheses:与所有运算符一样,优先级可以用括号覆盖:

int l, *m;
int n = l * (*m);

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

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