Three usages of C language 丨 arithmetic symbol & (with sample code)

&: There are two meanings in the C language. One is the address operator, which is a unary operator; the other is a bitwise operator, which means "bitwise AND", which is a binary operator.

 

1. Used for pointer assignment

#include<stdio.h>

int main ()

{

int a = 2;

int*b;//Define an integer pointer

b = &a;//Assign a pointer to make the pointer point to the address of a

printf("%d", b);//The output is the address of a

printf("\n");//Line break

printf("%d", *b);//*The function of dereference is to take out the contents of the pointer to the address to achieve simplicity

return 0;

}

2. Used for bitwise (bitwise operation) AND operation in binary operations

Monocular means that only one operand is needed, such as a++ a-- *a &a  

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

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

For example: 9&5 can be written as follows: 00001001 (9's two's complement) &00000101 (5's two's complement) 00000001 (1's two's complement) See 9&5=1.

The bitwise AND operation is usually used to clear certain bits or reserve certain bits. For example, clear the high eight bits of a to 0, and reserve the low eight bits 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. Used for logical AND when "&&" appears

To put it simply: logical and

To put it in a more popular way: and~~

&& is equivalent to the intersection in the set-for example: woman && man-no one on earth is a true value

PS: || is "logical or", "or"-for example: woman || man-everyone on the earth is satisfied

to sum up

1. Take the address du address operator. Monocular operation, combined with the dao variable or constant after it, is used to obtain the memory address of the operand.

2. The bitwise AND operation, the binocular operator, and the expressions on both sides of the & in the bit operation are involved in the operation.

When calculating, calculate by bit. For any bit, if the value of this bit in both operands is 1, the result is 1 in this bit, otherwise the result is 0 in this bit.

3. When two ampersands are used at the same time, that is, the ampersand, which represents the logical AND in logical operations. Binocular operators, the expressions on both sides of && are involved in the operation.

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

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

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

 

 

Finally, if you also want to become a programmer and want to quickly master programming, quickly join the learning penguin circle !

There are senior professional software development engineers who can answer all your doubts online ~ Introduction to programming language "so easy"

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112777709