[Detailed explanation of binary conversion and related operators]


Insert image description here

1. Binary and base conversion

We often hear the terms binary, octal, decimal and hexadecimal. What does that mean? In fact, binary, octal, decimal, and hexadecimal are just different representations of numerical values.
For example: various base representations of the value 12:

122进制:1100
128进制:14
1210进制:12
1216进制:C

Let’s focus on the binary system:
First of all, we have to start with the decimal system. In fact, the decimal system is often used in our lives. Many attempts have been made:

  • In base 10, every 10 is 1
  • Each digit of the decimal number isNumbers from 0 to 9composition

In fact, binary is the same

  • In binary system, full 2 ​​is entered into 1
  • Each digit of a binary number isNumbers from 0 to 1composition

2. Convert binary to 8, 10 or 16

2.1 Convert binary to decimal

In fact, the value represented by 123 in decimal system is one hundred and twenty-three. Why is this value? In fact, each digit of the decimal system has a weight, and the decimal system numbersright to left is the ones, tens, hundreds..., the weight of each digit is 100, 101, 102
as shown below: As shown below: If the binary number is 1101, how should we understand it? . 2, 21, 20
Insert image description here
The binary system is similar to the decimal system, except that the weight of the binary system from right to left is 2


Insert image description here

2.2 Convert binary to octal

Each digit of the octal number is a number from 0 to 7. Each digit is written in binary. A maximum of 3 binary digits is enough.
as follows:

0的二进制:000
1的二进制:001
2的二进制:010
3的二进制:011
4的二进制;100
5的二进制:101
6的二进制:110
7的二进制:111

Therefore, when converting a binary number to an octal number, start from the low bit on the right in the binary sequence and move to the left.Every 3 binary digits will be converted into an octal digit., remainingDirect conversion with less than 3 binary digits
For example: 01101011 in binary system, converted to octal system: 0153, numbers starting with 0 will be regarded as octal system.
Insert image description here

2.3 Convert binary to hexadecimal

Similar to converting from binary to octal
Each digit of a hexadecimal number is 0-9, A-F, if each digit is written in binary form , up to 4 binary bits are enough.
as follows:

0的二进制:0000			8的二进制:1000	
1的二进制:0001			9的二进制:1001
2的二进制:0010			A的二进制:1010
3的二进制:0011			B的二进制:1011
4的二进制;0100			C的二进制:1100
5的二进制:0101			D的二进制:1101
6的二进制:0110			E的二进制:1110
7的二进制:0111			F的二进制:1111

Therefore, when converting from binary to hexadecimal, start from the low bit on the right in the binary sequence and move to the left.Every 4 binary digits will be converted into a hexadecimal digit, remainingDirect conversion with less than 4 binary digits
For example: 01101011 in binary, change to hexadecimal: 0x6b, add 0x in front when expressed in hexadecimal
Insert image description here

3. Convert 8, 10, and 16 to binary

3.1 Convert decimal to binary

  • Method 1: Divide 2 by decimal number and record the result each timeremainder, if the obtained quotient is not 0, continue dividing until the obtained quotient is 0, and the obtained remainder from bottom to top is the binary converted from decimal. As shown below:

Insert image description here

  • Method 2: Patchwork method. This method requires memorizing the decimal numbers represented by the binary system.

20=1,21=2,22=4,23=8,24=16,25=32,26= 64,27=128,28=256,29=512,210=1024
For example: 125= 64+32+16+8+4+1
Therefore, the binary representation of 123 can be written like this: 1111101
Insert image description here

3.2 Convert octal to binary

To convert octal to binary, you only need to convert octal toBinary corresponding to 3 digitsThat’s it
For example: 153 converted to binary is 01 101 011.
Insert image description here

3.3 Convert hexadecimal to binary

To convert hexadecimal to binary, you only need to convert hexadecimal toBinary corresponding to 4 digitsThat’s it
For example: 6b converted to binary is: 0110 1011.
Insert image description here

4. Original code, inverse code, complement code

  • There are three representation methods of integer in binary:original code, complement code, and complement code
  • There are sign bits and in the three representation methods of signed integer , in the binary sequence, the highest 1 bit is regarded as the sign bit, and the rest are numeric bits. Numerical bits
  • The sign bits are all used0 represents "positive" and 1 represents "negative".
  1. positive integer has the same original, inverse and complement codes, as follows

Insert image description here

  1. The three representation methods of negative integer are different, and the change rules are as follows:
  • Original code: The original code is obtained by directly translating the value into binary in the form of positive and negative numbers.
  • Inverse code: Keep the sign bit of the original code unchanged, and the other bits in sequence. Invert to get the inverse code.
  • Return:Reverse +1Obtain the result.

Insert image description here
3. complement code converted to original code

  • Method 1: complement -1, then negate

Insert image description here

  • Method 2: Keep the sign bit unchanged, invert the other bits bit by bit, and then +1

Insert image description here

For integers: the data stored in the memory actually stores the complement code.

Why?
In computer systems, values ​​are always represented and stored using one's complement codes. The reason is that using the complement code, the sign bit and the numerical field can be processed uniformly; at the same time, addition and subtraction can also be processed uniformly (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 circuit is required

5. Shift operator (<< >>)

In memory, integers are stored in two's complement code, soWhen we operate on binary bits, we operate on two's complement code , and all we see is the original code
Note: The operand of the shift operator can only beinteger.

5.1 Left shift operator

Shift rules: discard on the left, add 0 on the right
Insert image description here
Insert image description here

5.2 Right shift operator

The right shift operator is divided intoLogical right shiftandarithmetic right shift(Most compilers use arithmetic right shift)

  1. Logical right shift: Use the left0 padding, discard on the right
  2. Arithmetic right shift: Use the original value on the leftSign bit stuffing, discard on the right

Insert image description here

Insert image description here
Insert image description here

Note:For the shift operator, Do not move negative bits, this is undefined by the standard of.

6. Bit operators (& | ^ ~)

Their operands must also be integers

6.1 &

Bitwise AND principle: perform operations on the corresponding binary bits,It is 1 if both are 1, otherwise it is 0
Insert image description here

6.2 |

Bitwise OR principle: perform operations corresponding to binary,As long as there is 1, it is 1, and if all are 0, it is 0
Insert image description here

6.3 ^

The principle of bitwise XOR:The same value is 0, and the difference is 1.
Insert image description here

6.4 ~

Bitwise negation principle:All bits are directly inverted
Insert image description here

7.Related topics

  1. Temporary variables (third variables) cannot be created to achieve the exchange of two numbers.

First of all, you need to understand something:0^0=0 a^0=a; a^a=0 That is:A number is exclusive-or with 0 and is itself. Two identical numbers are exclusive-or with 0.

int main()
{
    
    
	int a = 3;
	int b = 5;
	printf("a=%d b=%d\n", a, b);
	a = a ^ b;
	b = a ^ b;  //(a^b^b)
	a = a ^ b;	//(a^a^b)
	printf("a=%d b=%d\n", a, b);
	return 0;
}

Insert image description here

  1. Find the number of binary 1's in an integer stored in memory

Method 1: Bitwise AND with 1 is not equal to

a & 1 = 1; the lowest bit of the binary digit of a is 1
a & 1 = 0; the lowest bit of the binary digit of a is not 1

int main()
{
    
    
	//00000000000000000000000000000001   1
	
	//10000000000000000000000000000001   -1
	//11111111111111111111111111111110  -1反码
	 
	//11111111111111111111111111111111   -1补码
	//00000000000000000000000000000001
	//00000000000000000000000000000001
	//n 的二进制位的最低位&1,如果结果是1,那么就说明最低位是1
	//所以,我们要想办法让1动起来
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	for (int i = 0; i < 32; i++)
	{
    
    
		if ((n & (1 << i)) != 0 )
		{
    
    
			count++;
		}
	}
	printf("%d\n", count);
	return 0;
}

Method 2: Based on the formula n = n & (n-1)

int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
    
    
		n = n & (n - 1);
		count++;
	}
	printf("%d\n", count);
	return 0;
}
  1. Write code to change the 5th bit of the 13-binary sequence to 1, and then change it back to 0
int main()
{
    
    
	int n = 13;
	n = n | (1 << 4);
	printf("%d\n", n);
	//000000000000000000000000000001011
	//000000000000000000000000000010000   1<<4
	//000000000000000000000000000011011
	
	n = n & (~(1 << 4));
	printf("%d\n", n);
	//000000000000000000000000000011011
	//111111111111111111111111111101111    ~(1<<4)
	//000000000000000000000000000001011
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_69380220/article/details/134244500
Recommended