Three usages of C language – operation symbols & (with example code)

Time:2021-8-23

&: there are two meanings in C language. One is to take the address symbol, which is a monocular operator; The other is a bitwise operator, which means “bitwise and”, which is a binocular operator.

 

1. For pointer assignment

#include

int main()

{

int a = 2;

int*b;// Defines an integer pointer

b = &a;// Assign a value to the pointer so that the pointer points to the address of A

printf(“%d”, b);// The output is the address of A

printf(“\n”);// Newline character

printf(“%d”, *b);//* The function of is to dereference, take out the content of the address pointed by the pointer, and achieve simplicity

return 0;

}

2. Bitwise (bitwise) and operations in binomial operations

Singleton means that only one operand is required, such as a + + a–    * a   & a

Binocular means that two operands are required    such as    a+b   a-b a*b   a/b   a%b

Three items means that three operands are required    such as     a=c>b?c:b;

For example, 9 & 5 can be written as follows: 00001001 (binary complement of 9) & 00000101 (binary complement of 5) 00000001 (binary complement of 1). 9 & 5 = 1.

Bitwise and operations are usually used to clear 0 or reserve some bits. For example, clearing the upper eight bits of a to 0 and reserving the lower eight bits can be used for a-255 operation (the binary number of 255 is 0000000011111111).

main(){

int a=9,b=5,c;

c=a&b;

printf(“a=%d/nb=%d/nc=%d/n”,a,b,c);

}

3. When “& &” appears, it is used for logic and

To put it simply: logic and

The popular point is: and~~

&&It is equivalent to the intersection in the set – for example, women & & men – no one on earth is true

PS: ||   It is “logical or” or “- for example, women and men – everyone on earth is satisfied

summary

1. Take Du address operator. A unary operation, which is combined with its subsequent Dao variables or constants, to obtain the memory address of the operand.

2. Bitwise and operations in bitwise operations, binocular operators, and expressions on both sides of &.

During calculation, it is calculated by bit. For any bit, if both operands have a value of 1, the result is 1; otherwise, the result is 0

3. When two & symbols are used at the same time, i.e. & & symbol, it represents logical and in logical operation. Binocular operator, expressions on both sides of & & participate in the operation.

When && both sides are true, the result is true. Otherwise, the result is false.

&&Because it is a logical operation, the result is only true (1) or false (0).

This is the end of this article about the usage examples of & in C language. If you have any questions, please leave a message in the comment area.

 

 

Finally, if you want to be a programmer and want to master programming quickly, join quicklyLearn Penguin circle

There are senior professional software development engineers to answer all your doubts online ~ introduction to programming language “so easy”

Programming learning books:

 

Programming learning video: