Detailed explanation of operators (very detailed)

binary introduction

When we first learn computers, we often hear binary, octal, decimal, hexadecimal... These sound very advanced words, in fact, octal, decimal, and hexadecimal are almost the same as binary are very similar, with only slight differences, which we describe in more detail below

binary

Let's take an example, such as using different base forms to represent the value 15:

152进制:1111
158进制:17
1510进制:15
1516进制:F

Let's start with the familiar base 10:
in the addition and subtraction we have learned, we often use this method to calculate:
insert image description here
since the ones digit is added to 10, 1 is added to the tens digit, and the tens digit It is also full of tens, so we add 1 to the hundreds digit
, so we can know from here that the so-called decimal system is actually full of 10 and 1, and each digit cannot exceed 10. In
fact, other bases are the same, such as binary system
2 The base system is full 2
​​and 1. Each digit of the binary system is composed of numbers from 0 to 1,
so 1011 is the binary number of 15

Binary to decimal

The value represented by 123 in decimal is one hundred and twenty-three. Why is it this value? In fact, each digit in the decimal system has a weight, and the numbers in the decimal system are ones, tens, hundreds... from right to left, and the weight of each digit is 10^0, 10^1, 10^2...
As shown in the figure below:
insert image description here
Binary and decimal are similar, except that the weight of each bit of binary, from right to left: 2^0, 2^1, 2^2...
If it is binary 1101, how to understand it?
insert image description here

Decimal to binary numbers

The method is as shown in the figure:
insert image description herewe only need to divide 125 by 2 to get the number of 125 in binary system ( in fact, I haven't fully understood why this can get the result, so if I figure it out later, I will post it here Plus your own understanding )

Binary to octal and hexadecimal

Binary to Octal

Each digit of the octal number is 0~7, and the numbers of 0~7 are written in binary system, and a maximum of 3 binary digits is enough . For example, the binary number of 7 is 111, so in the binary system When converting an octal number, every 3 binary digits will be converted into an octal digit starting from the lower right bit in the binary sequence to the left, and the direct conversion of the remaining 3 binary digits is as follows:
binary The system 01101011 is replaced by octal: 0153, the array starting with 0 will be regarded as octal
insert image description here

Binary to hexadecimal

Each digit of the hexadecimal number is 0~9, a~f (10~15), each written in binary, up to 4 binary digits are enough. For example, the binary number of f is 1111, so
in When converting binary numbers to hexadecimal numbers, every 4 binary digits will be converted to a hexadecimal digit starting from the lower right bit in the binary sequence to the left, and the direct conversion of the remaining 4 binary digits is as follows:
2 01101011 in hexadecimal, change to hexadecimal: 0x6b, add 0x in front of hexadecimal
insert image description here

Original code, inverse code, complement code

There are three binary representation methods of integers, namely original code, inverse code and complement code. All
three representation methods have a sign bit and a value bit. The sign bit uses 0 to represent "positive" and 1 to represent "negative". , and the highest bit of the value is regarded as a sign bit, and the rest are value bits.
The original, inverse, and complement codes of positive integers are the same.
There are three different ways of representing negative integers.
Original code: The original code is obtained by directly translating the value into binary in the form of positive and negative numbers.
Inverse code: The sign bit of the original code remains unchanged, and the other bits are sequentially inverted to obtain the inverse code.
Complement code: Inverse code + 1 to get complement code

For shaping: the data stored in the memory is actually stored in the complement code
because in the computer system, the values ​​are all expressed and stored in the complement code . The reason is that, using the complement code, the sign bit and the value field can be processed in a unified manner. At the same time, addition and subtraction can also be processed in a unified manner (the CPU only has an adder). In addition, the complement code and the original code are converted to each other, and the operation process is the same , no additional hardware circuitry is required

shift operator

The operation symbols are as follows:
<<Left Shift Operator
>>Right Shift Operator
Note: The operand of the shift operator can only be an integer ( because the original code, inverse code, and complement code refer to the binary system of integers, If it is a decimal, it cannot be expressed ) ( but I remember that there seems to be a way to express decimals, but I don’t know if it can be used here, maybe I didn’t figure it out )

left shift operator

insert image description here

right shift operator

Shift rules: First, there are two types of right shift operations:
1. Logical right shift: the left is filled with 0, and the right is discarded
insert image description here

2. Arithmetic right shift: the left side is filled with the sign bit of the original value, and the right side is discarded.
insert image description here
Special reminder : For shift operators, do not move negative digits. This is undefined by the standard (and it is not necessary)
For example:

int num = 10;
num>>-1;//error

Bitwise operators: &, |, ^

Bitwise operators are:

1.& //按位与    对应位同时为“1”时才为“1”,否则为0
2.| //按位或    对应位只要有一个为1就为1
3.^ //按位异或   对应位相同为 0,不同为 1。
注:他们的操作数必须是整数

code show as below:

#include <stdio.h>
int main()
{
    
    
		int num1 = -3;
		int num2 = 5;
		num1 & num2;
		num1 | num2;
		num1 ^ num2;
return 0;
}

Here is an interview question:
Cannot create a temporary variable (the third variable), to realize the exchange of two numbers
Method 1:

可能有很多人都会这样做
#include <stdio.h>
int main()
{
    
    
int a = 10;
int b = 20;
a = a+b;
b = a-b;
a = a-b;
printf("a = %d b = %d\n", a, b);
return 0;
}

This method has a flaw, that is, if a+b is too large, it will cause an error, because int has a range limit, so this method should be used according to the situation. Let's look at the
second method:

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

This method is hard to think of, but it also has flaws, that is, if there are negative numbers, the result will be problematic, so this method is only applicable to positive numbers

Example: Write code to achieve: Find the number of 1s in binary stored in memory by an integer:

//⽅法1
#include <stdio.h>
int main()
{
    
    
int num = 10;
int count= 0;//计数
while(num)
{
    
    
if(num%2 == 1)
count++;
num = num/2;
}
printf("⼆进制中1的个数 = %d\n", count);
return 0;
}
//⽅法2:
#include <stdio.h>
int main()
{
    
    
int num = -1;
int i = 0;
int count = 0;//计数
for(i=0; i<32; i++)
{
    
    
if( num & (1 << i) )
count++;
}
printf("⼆进制中1的个数 = %d\n",count);
return 0;
}
//⽅法3:
#include <stdio.h>
int main()
{
    
    
int num = -1;
int i = 0;
int count = 0;//计数
while(num)
{
    
    
count++;
num = num&(num-1);
}
printf("⼆进制中1的个数 = %d\n",count);
return 0;
}

comma expression

Comma expressions are multiple expressions separated by commas.
Comma expressions are executed sequentially from left to right. The result of the entire expression is the result of the last expression. (If the expression on the left is false, the expression on the right is not executed)

//代码1
int a = 1;
int b = 2;
int c = (a>b, a=b+10, a, b=a+1);//逗号表达式
//代码2
if (a =b + 1, c=a / 2, d > 0)
while (a = get_val(), count_val(a), a>0)
{
    
    
..........
}

Guess you like

Origin blog.csdn.net/2301_79178723/article/details/132363154
Recommended