Operator explanation

Posted by samusk on Sat, 15 Jan 2022 13:11:41 +0100

1, Arithmetic operator

There are five arithmetic operators: + - * /%. Because the operator has two operands. Also known as binocular operator

For the / operator, both operands are integers and perform integer division. Performs floating-point division when at least one operand is floating-point

#include <stdio.h>
int main() {
int i = 5 / 2;
printf("%d\n", i);
return 0;
}

output

2
#include <stdio.h>
int main() {
double i = 5.0 / 2;
printf("%lf\n", i);
return 0;
}

output

2.500000

%The two operands of the operator must be integers, and the remainder after division is returned

#include <stdio.h>
int main() {
int i = 20 % 6;
printf("%d\n", i);
return 0;
}

For a / b, when one of the two operands of the operator is negative. If the left operand is negative, the result is: - (- a) / b). If the right operand is negative, the result is: - (a / (-b))

#include <stdio.h>
int main() {
int i = -7 / 3;
int j = 5 / -2;
printf("-7 / 3 = %d\n", i);
printf("5 / -2 = %d\n", j);
return 0;
}

output

-7 / 3 = -2
5 / -2 = -2

For a% B operator, when one of the two operands is negative. If the left operand is negative, the result is: - (- a)% B). If the right operand is negative, the result is: a% (- b)

#include <stdio.h>
int main() {
int i = -7 % 2;
int j = 5 % -2;
printf("-7 %% 2 = %d\n", i);
printf("5 %% -2 = %d\n", j);
return 0;
}

output

-7 % 2 = -1
5 % -2 = 1

2, Shift operator

Integers are binary stored in memory. When storing signed integers, set the highest bit of the binary to the sign bit. 1 represents a negative number and 0 represents a positive number. The binary representation of an integer includes original code, inverse code and complement code. The original code, inverse code and complement of positive integers are the same. The original code, inverse code and complement of negative integers need to be calculated. The original code is used when using and printing integers, and the complement code is used when calculating

int i = 10; //The original code, inverse code and complement of positive integers are the same
00000000 00000000 00000000 00001010  //Original code of 10
00000000 00000000 00000000 00001010  //Inverse code of 10
00000000 00000000 00000000 00001010  //Complement of 10

int j = -1
10000000 00000000 00000000 00000001  //-The original code of 1, that is - 1 into binary, and the highest bit uses 1 to represent a negative number
11111111 11111111 11111111 11111110 //-The sign bit of the inverse code of 1 remains unchanged, and other bits of the original code are reversed by bit
11111111 11111111 11111111 11111111 //-Complement of 1, inverse + 1

2.1 shift left operator<<

Usage: number < < n
The complement of the number moves n bits to the left, that is, discard on the left and fill 0 on the right

#include <stdio.h>
int main() {
int i = 10 << 2;
//00000000 00000000 00000000 00001001 original code
//00000000 00000000 00000000 00001001 inverse code
//00000000 00000000 00000000 00001001 complement
//00000000 00000000 00000000 00100100 shift the complement by two digits to the left and two zeros to the right
printf("%d\n", i);
return 0;
}

output

40
#include <stdio.h>
int main() {
int i = -1 << 2;
//10000000 0000000 00000000 00000001 - 1 original code
//11111111111111111111111111111111110 - 1 inverse code
//11111111111111111111111111111111111111111111 - 1 Complement
//11111111111111111111111111111111111100 pairs of complements are shifted by 2 bits to the left and 2 zeros to the right

//11111111111111111111111111111111011 inverse code, complement - 1 to the above
//10000000 0000000 0000000 00000100 original code, the sign bit of inverse code remains unchanged, and other bits are reversed by bit
printf("%d\n", i);
return 0;
}

output

-4

2.2 shift right operator > >

Usage: number > > n
The complement of the number is shifted to the right by n bits, that is, the right is discarded and the left is filled
According to the filling rules, there are two kinds: logical shift and arithmetic shift
Logical shift: fill the left with 0
Arithmetic shift: the left side is filled with the digit sign bit
Most compilers use arithmetic shifts. The compiler we demonstrate is also arithmetic shift

#include <stdio.h>
int main() {
int i = -1 >> 2;
//10000000 0000000 00000000 00000001 - 1 original code
//11111111111111111111111111111111110 - 1 inverse code
//11111111111111111111111111111111111111111111 - 1 Complement
//111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

//The calculated result is still the complement of - 1, so it is restored to the original code and no longer written
printf("%d\n", i);
return 0;
}

output

-1
#include <stdio.h>
int main() {
int i =10 >> 2;
//00000000 00000000 00000000 00001010 original code
//00000000 00000000 00000000 00001010 10 inverse code
//00000000 00000000 00000000 00001010 10 complement
//00000000 00000000 00000000 00000000 00000010 the complement is shifted by 2 digits to the right, and all the complement symbols on the left are 0
printf("%d\n", i);
return 0;
}

output

2

For shift operators, do not move negative digits, which is not defined in the C language standard
If int i = 10 > > - 1;

3, Bitwise operator

There are three kinds of bitwise operators: bitwise AND & bitwise or | bitwise XOR ^

3.1 bitwise AND&

Usage: num1 & num2
Calculate the complement of two numbers. When the binary corresponding to both numbers is 1, the result is 1, otherwise it is 0

#include <stdio.h>
int main() {
int i = 10 & -3;
//00000000 00000000 00000000 00000000 00001010 original code, inverse code and complement code

//Original code of 10000000 0000000 00000000 00000011 - 3
//11111111111111111111111111111111111100 - inverse code of 3
//11111111111111111111111111111111111101 - 3 Complement

//00000000 00000000 00000000 00001010 10 complement
//11111111111111111111111111111111111101 - 3 Complement
//00000000 00000000 00000000 00000000 00001000 the corresponding binary bits are 1, the result is 1, otherwise it is 0
printf("%d\n", i);
return 0;
}

output

8

3.2 bitwise OR|

Usage: num1 | num2
Calculate the complement of two numbers. When at least one binary corresponding to two numbers is 1, the result is 1, otherwise it is 0

#include <stdio.h>
int main() {
int i = -4 | -3;
//Original code of 10000000 0000000 0000000 - 4
//11111111111111111111111111111111111011 - 4
//11111111111111111111111111111111100 - 4 complement

//Original code of 10000000 0000000 00000000 00000011 - 3
//11111111111111111111111111111111111100 - inverse code of 3
//11111111111111111111111111111111111101 - 3 Complement

//11111111111111111111111111111111100 - 4 complement
//11111111111111111111111111111111111101 - 3 Complement
//11111111111111111111111111111111111111111111111101 corresponds to at least one binary bit. When it is 1, the result is 1, otherwise it is 0 (this result is a complement)
//11111111111111111111111111111111100 inverse code, complement-1
//10000000 00000000 00000000 00000011 original code, the sign bit of inverse code remains unchanged, and other bits are reversed by bit
printf("%d\n", i);
return 0;
}

output

-3

3.3 bitwise XOR^

Usage: num1 ^ num2
Calculate the complement of two numbers. If the binary phases corresponding to the two numbers are the same, the result is 0, otherwise it is 1

#include <stdio.h>
int main() {
int i = -4 ^ -3;
//Original code of 10000000 0000000 0000000 - 4
//11111111111111111111111111111111111011 - 4
//11111111111111111111111111111111100 - 4 complement

//Original code of 10000000 0000000 00000000 00000011 - 3
//11111111111111111111111111111111111100 - inverse code of 3
//11111111111111111111111111111111111101 - 3 Complement

//11111111111111111111111111111111100 - 4 complement
//11111111111111111111111111111111111101 - 3 Complement
//00000000 00000000 00000000 00000001 corresponds to the same binary phase, the result is 0, otherwise it is 1

printf("%d\n", i);
return 0;
}

output

1

3.4 classic written test questions

Title: there are two integer variables A and b. do not create temporary variables to exchange the values of a and b

Method 1

#include <stdio.h>
//When the sum of two numbers is greater than the int type, there is an overflow and this method cannot be used
int main() {
int a = 10;
int b = 20;
printf("Before exchange a = %d b = %d\n",a,b);
a = a + b;
b = a - b;
a = a - b;
printf("After exchange a = %d b = %d\n", a, b);
return 0;
}

output

Before exchange a = 10 b = 20
After exchange a = 20 b = 10

Method 2

#include <stdio.h>
int main() { //This method is only applicable to the exchange between two integers
int a = 10;
int b = 20;
printf("Before exchange a = %d b = %d\n",a,b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("Before exchange a = %d b = %d\n", a, b);
return 0;
}

output

Before exchange a = 10 b = 20
Before exchange a = 20 b = 10

Topics: C C++