Knowledge point 2: Operator

Operator

1,After the operator is a whole
Such as: a*=b+c; is equivalent to a=a*(b+c);

2. The result of the relational operation is Boolean. Boolean (bool) values ​​have only two values: 1 and 0, that is, true and false.

3. When calculating a logical expression, the expression is solved only when the next expression must be executed to be solved. In other words, not all expressions are executed.

4. The comma operator (sequential evaluation operator).
The comma operator has the lowest precedence, and the combination direction is from left to right. Its function is: connect two expressions to form an expression (called a comma expression).
The general format is: 表达式1, 表达式2;
the evaluation process is to first solve expression 1, then solve expression 2, and use the value of expression 2 as the value of the entire comma expression.
Generalization: 表达式1, 表达式2, 表达式3,... ,表达式n;
Use the value of the expression n as the value of the entire comma expression.
E.g:

int a = 4, b = 6, c = 8, r1, r2;
r1 = a + b, r2 = b + c;
printf("%d, %d", r1, r2);//输出10,14
//本例中r2等于整个逗号表达式的值,即表达式2的值;r1是第一个表达式的值。

For example: x = 1, y = 2, z = 3;and x = 1; y = 2; z = 3;equivalent
. The use of a comma expression in a program usually requires the value of each expression in the comma expression to be evaluated separately, and it does not necessarily require the value of the entire comma expression.

int a, b, c, d;
c = (a = 3, 2 * a);//将一个逗号表达式的值赋给c
d = b = 3, 2 * a;//逗号仅作为分隔符
printf("%d,%d,%d,%d\n", a, b, c, d);//3,3,6,3
return 0;

5. Bit operation

Pay attention to the byte number of the data type
1. The bitwise "or" operation "|" The
bitwise OR operator "|" is a binocular operator, its function is to make the two numbers involved in the operation correspond to the binary phase OR: as long as If there is a 1 in the corresponding bit of the two numbers, the result is 1 in that bit.

For example: 3 | 7 the formula is:
0000 0011
0000 0111
—————
0000 0111
Another example: 64 | 63 the formula is:
0100 0000
0011 1111
——————
0111 1111

From the above calculation process, it can be seen that if you want to make the last 6 digits of a binary number all 1, you only need to OR with 63; in the same way, if you want to make the last 5 digits all 1, you only need to OR with 31. That's it.
Summary: If you want to set a certain number to 1, you only need to perform a bitwise OR operation with these numbers that are 1.
2. Bitwise "and" operation "&"
Bitwise "and" operator & is a binocular operator, its function is: make the two numbers involved in the operation correspond to the binary bit phase and: only two corresponding binary bits are equal When it is 1, the result bit is 1.

For example: 13 & 18 the formula is:
0000 1101
0001 0010
—————
0000 0000
can be seen, one purpose of bitwise AND operation is: clearing. If the bit that is 1 in the original number is 0, you only need to AND the bit that is 0.
Another example: 31 & 22 the formula is:
0001 1111
0001 0110
—————
0001 0110

You can take some specified bits of a number by bitwise AND. For example, if you want to take the last 5 digits of 22, you need to AND a number whose last 5 digits are all 1.
3. Inversion operation "~" The
inversion operator "~" is a monocular operator, with right associativity, and its function is to invert the binary form of the number participating in the operation bit by bit: change 0 to 1, change 1 becomes 0.
For example: ~19the formula is:
0000 0000 0001 0011 —> 1111 1111 1110 1100
(note the number of bytes occupied by the data type)
Note that the leftmost bit of a signed number is the sign bit. After the inversion, the positive and negative will change. But the negative result is not the opposite of the original number!
(If you invert a number, and add 1 to the end, it will be equal to 0 after adding to the original number, which is the storage method of negative numbers: Invert a number +000000001 = its opposite complement)
4, "Exclusive OR" operation "^"
Bitwise XOR operator "^" is a binary operator, its function is to make the corresponding binary bits of the two numbers involved in the operation XOR: the result is 1 when the difference is different, and the result is the same when the result is the same Is 0.

For example: 44 ^ 25 the formula is:
0010 1100
0001 1001
—————
0011 0101

One of the main uses of the XOR operation is:Invert a specific bit

For example: To reverse the last 4 digits of 69:
0100 0101
0000 1111
—————
0100 1010
Summary: Which bits you want to invert are XORed with the numbers whose bits are 1, and the rest are 0

Another use of the XOR operation is:Realize the exchange of two variable values ​​without using temporary variables.
It can be achieved as follows:

x = x ^ y;
y = x ^ y;
x = x ^ y;

The XOR operation is often used in some relatively simple encryption operations.
5. "Left shift" and "Right shift" operations "<< "," >> "
Left shift and right shift operators are all binocular operators.
The function of "<<" is: shift all the binary bits of the operand on the left of "<<" by a few digits to the left, and the number on the right of "<<" specifies the number of digits to move, the high digit is discarded, and the low digit is filled with 0. (Pay attention to the number of bytes of the data type!)
For example: a<<2 means that the binary bits of a are shifted by 2 bits to the left.
Assuming a=0000 0110, it is 0001 1000 after shifting two bits to the left. a changed from 6 to 24.
Note: When the shifted out digit does not contain 1, shifting to the left by 1 digit is equivalent to multiplying the number by 2; shifting to the left by 2 digits is equivalent to multiplying the number by 4...
The function of ">>" is: the operation to the left of ">>" The binary digits of the number are all shifted to the right by several digits, and the number to the right of ">>" specifies the number of digits to move.
When shifting to the right, for signed numbers, pay special attention to the sign bit:
when it is a positive number, the highest bit is filled with 0;
when it is a negative number, whether the highest bit is filled with 0 or 1 depends on the regulations of the compiler system. (The complement of 0 is called "logical shift right", and the complement of 1 is called "arithmetic shift right".)

//循环右移
//将x的右端n位先放到z中的高n位中
z = x << (16 - n);
//将x右移n位,其左边高n位补0
y = x >> n;
//将y与z进行 按位或运算
z = z | y;

//综上即:
z = x << (16-n) | x >> n;
//循环移位
//从键盘输入一个八进制数,然后输入要移动的位数(正数表示向右移位,负数表示向左移位),输出移位后的结果。
//Xcode里整型数据占两个字节
#include <stdio.h>

int right(unsigned value, int n)
{
    
    
    unsigned z;
    z = (value >> n) | (value << (16 - n));//循环右移的实现过程
    return (z);
}

int left(unsigned value, int n)
{
    
    
    unsigned z;
    z = (value >> (16-n)) | (value << n);//循环左移的实现过程
    return (z);
}

int main()
{
    
    
    unsigned a;
    int n;
    scanf("%o%d", &a, &n);//输入一个八进制数和要移动的位数
    if (n > 0) printf("%o", right(a, n));
    else
    {
    
    
        n = -n;
        printf("%o\n", left(a, n));
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Shao_yihao/article/details/112788024