[C Language Elementary] Take you to easily play with all common operators (1) - arithmetic operators, shift operators, bit operators

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development


Hello, this is Junxi_. I am going to prepare for the final review recently. Maybe the update will not be so frequent, and the normal update will resume next week.

foreword

What I bring to you today is a detailed explanation of operators. Since this part has a lot of content, it may be divided into several parts. Today I bring you the content of the first part. Let’s start without talking nonsense!

1. Classification of operators

  • Operators are roughly divided into the following types
  • arithmetic operator
  • shift operator
  • bitwise operator
  • assignment operator
  • unary operator
  • relational operator
  • logical operator
  • conditional operator
  • comma expression
  • Subscript references, function calls, and structure members
  • Let's draw a picture first to illustrate, and then introduce the usage and usage scenarios one by one.
    insert image description here

2. Arithmetic operators

   +   -  *  /  %
    1. In addition to the % operator, several other operators work on integers and floating-point numbers.
    1. For the / operator, if both operands are integers, integer division is performed. And as long as there are floating-point numbers, floating-point number division is performed.
    1. Both operands of the % operator must be integers. Returns the remainder after division.
  • Let's understand it through a piece of code:
#include<stdio.h>
int main()
{
    
    
	int r = 7 / 2;
	printf("%d\n", r);
	double d = 7 / 2;
	printf("%lf\n", d);//3.5?
	double d1 = 7.0 / 2.0;
	printf("%lf\n", d1);//3.5?

	return 0;
}
  • operation result
    insert image description here
  • You may have no doubts about the first number and the third number printed. The only problem is why the decimal part after the second number is omitted. Next, I will talk about the reason
  • We can see that 7 and 2 are both integers, so integer division is performed. There are no decimals in plastic surgery, so the part after the decimal point will be directly discarded. At this time, we assign the 3 obtained by 7/2 to a floating-point d, and the printed out is to keep the digits after the decimal, so the printed result is the result on the screen.
  • Summarize:
  • When you want to perform floating-point division, the numbers on both sides of the division sign must have at least one floating-point type! ! !

3. Shift operator

<< 左移操作符
>> 右移操作符
 
注:移位操作符的操作数只能是整数。

Integers are stored in binary form in memory, while those stored in memory are two's complement and stored upside down.

Now let me explain the above passage in detail.

1. Three forms of binary representation

  • 二进制在内存中表示形式分别是原码,反码,补码。
  • Among them, the data is stored in the memory in the form of two's complement
  • Illustrate with the following code

int main()
{
    
    
	
	int num  = 10;//创建一个整型变量,叫num,这时num向内存申请4个字节来存放数据
	//4个字节-32比特位
	//00000000000000000000000000001010-原码
	//00000000000000000000000000001010-反码
	//00000000000000000000000000001010-补码

	int num2 = -10;//
	//10000000000000000000000000001010 - 原码
	//11111111111111111111111111110101 - 反码
	//11111111111111111111111111110110 - 补码
	


	return 0;
}
  • The binary representation of decimal data is the original code, the leftmost number of the original code is the sign bit, 0 is positive, and 1 is negative.
  • For positive numbers, the complement of the original code is the same as the three complements. For negative numbers, the inverse code is that the sign bit of the original code remains unchanged, and the other bits are reversed (1 becomes 0, 0 becomes 1)
  • For negative numbers, the complement is one's complement + 1.

2. Left shift operator <<

  • shift rule
    左边抛弃,右边补零
  • Note that below we speak of shifting,Shifts are all two's complement binary sequences
int main()
{
    
    

	int num = 10;//创建一个整型变量,叫num,这时num向内存申请4个字节来存放数据
	//4个字节-32比特位
	//00000000000000000000000000001010-原码
	//00000000000000000000000000001010-反码
	//00000000000000000000000000001010-补码
	int b=num << 1;
	//00000000000000000000000000010100-补码
	printf("%d\n", num);
	printf("%d\n", b);

	
	return 0;
}

insert image description here

  • Note that the shift operation does not change the size of the original variable! !

3. Right shift operator >>

  • Shift rules:
  • First, there are two types of right shift operations:
  • 1. Logical shift
    左边用0填充,右边丢弃
  • 2. Arithmetic shift
    左边用原该值的符号位填充,右边丢弃
int main()
{
    
    

	int num = 10;
	
	//逻辑右移 左边用0填充,右边丢弃
	//00000000000000000000000000001010-补码
	int b=num >> 1;
	//00000000000000000000000000000101-补码
	printf("%d\n", num);
	printf("%d\n", b);
	//算数右移 左边用原该值的符号位填充,右边丢弃
	int num1 = -1;
	//10000000000000000000000000000001-原码
	//11111111111111111111111111111110-反码
	//11111111111111111111111111111111-补码
	int c = num1 >> 1;
	printf("%d", c);
	
	return 0;
}


  • The C language does not clearly stipulate whether it is a logical right shift or an arithmetic right shift, but the general editor uses an arithmetic right shift
    insert image description here
  • 如图可见,我们当前编译器采用的是逻辑右移,否则符号位应该变为0打印出来的应该是一个正数。

4. Wrong wording

  • In addition, pay attention to this way of writing:
int main()
{
    
    
	int a = 5;
	int b = a >> -2;//标准未定义行为

	return 0;
}

  • Some people may have a brain twitch and suddenly think, isn't it a left shift if I shift a negative number to the right?
  • Note: For shift operators, don't shift negative bits, this is standard undefined behavior! !

Four. Bitwise operators

  • Bitwise operators are:
 & //按位与
 | //按位或
 ^ //按位异或
  • Note: Their operands must be integers .
  • The corresponding operation of the following bit operators is still two's complement

1.& bitwise AND

& Bitwise AND---- If the corresponding binary bit is 0, it is 0, and if both are 1 at the same time, it is 1

int main()
//{
    
    
//	int a = 3;
//	//00000000000000000000000000000011 - 补码
//	int b = -5;
//	//10000000000000000000000000000101
//	//11111111111111111111111111111010
//	//11111111111111111111111111111011 - 补码
//	//
//	int c = a & b;
//	//& -- 对应二进制位有0则为0,两个同时为1,才是1
//	//00000000000000000000000000000011
//	//11111111111111111111111111111011
//	//00000000000000000000000000000011 - 补码
//	//
//	printf("%d\n", c);//3
//
//	return 0;
//}

insert image description here


2. | bitwise or

|Bitwise OR---- If the corresponding binary bit has 1, it is 1, and if two of them are 0 at the same time, it is 0

int main()
{
    
    
	int a = 3;
	//00000000000000000000000000000011 - 补码
	int b = -5;
	//10000000000000000000000000000101
	//11111111111111111111111111111010
	//11111111111111111111111111111011 - 补码
	//
	int c = a | b;
	//| - 按(2进制)位或 - 对应的二进制位有1则为1,两个同时为0才是0
	//00000000000000000000000000000011
	//11111111111111111111111111111011
	//11111111111111111111111111111011 - 补码
	//11111111111111111111111111111010
	//10000000000000000000000000000101 - -5
	printf("%d\n", c);//3

	return 0;
}

insert image description here


3.^ Bitwise XOR

^ Bitwise XOR----the corresponding binary bits are the same as 0, and the difference is 1

int main()
{
    
    
	int a = 3;
	//00000000000000000000000000000011 - 补码
	int b = -5;
	//10000000000000000000000000000101
	//11111111111111111111111111111010
	//11111111111111111111111111111011 - 补码
	//
	int c = a ^ b;
	//^ - 按二进制位异或 -对应的二进制位相同为0,相异为1
	//00000000000000000000000000000011
	//11111111111111111111111111111011
	//11111111111111111111111111111000 - 补码
	//11111111111111111111111111110111
	//10000000000000000000000000001000 - -8
	printf("%d\n", c);//3

	return 0;
}

insert image description here


4. Application examples of bit operators

  • After reading the above content, some people may have doubts, what are the specific uses of these bit operators?
  • Let's illustrate with a few examples:
  • 1. Let's look at a simple example first :
int main()
{
    
    
int num1 = 1;
int num2 = 2;
num1 & num2;
num1 | num2;
num1 ^ num2;
return 0;
}
  • What is the result of a,b,c in the above code?
    insert image description here
  • very simple, right? Don't be lazy when you encounter this kind of problem, drawing pictures is the best solution!
  • 2. Realize the exchange of two numbers without creating a temporary variable (the third variable).
  • This is actually an interview question for a certain company. If you can understand the content mentioned above and learn to apply it, it is actually not complicated.
  • The implementation code is as follows:
//在不创建临时变量(第三个变量)的情况下,实现两个数的交换。
int main()
{
    
    
	int a = 10;
	00000000000000000000000000001010--10的补码
	int b = 20;
	00000000000000000000000000010100--20的补码
	a = a ^b;
	b = a ^ b;
	a = a ^ b;
	printf("a=%d b=%d", a, b);
	return 0;
}

insert image description here

  • Successfully achieved our goal, but why?
  • Let me explain it below. If you are a beginner who is not particularly good at the basics, it is still recommended to draw a picture first to feel it!
	a = a ^b;
	//00000000000000000000000000001010--a
	//00000000000000000000000000010100--b
	//00000000000000000000000000011110--a=a^b
  • At this point a has become a ^ b

	b = a ^ b;
	//00000000000000000000000000010100--b
	//00000000000000000000000000011110--a=a^b
	//00000000000000000000000000001010-b=a^b    
  • At this time, b has been exchanged with a and becomes 10
a = a ^ b;
	//00000000000000000000000000011110--a=a^b
	//00000000000000000000000000001010-b=a^b 
	//00000000000000000000000000010100 a=a^b a=20
  • The last XOR, exchange the value of b to a
  • Is it clearer to analyze this code step by step?

Summarize

  • Today's content is over here for the time being. Today we talked about the application of arithmetic operators, shift operators and bit operators. If you are still confused, you may wish to do it yourself. The knowledge in this area is a bit abstract and requires repeated practice to get familiar with it.
  • The above is the first part of the content about operators, and the detailed explanations of other operators will be updated in batches in the near future, so stay tuned! !
  • Well, if you have any questions, please ask me in the comment area or private message, see you next time!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/131365364