C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Click the business card to follow us

C++ GESP Level 3 Key Points: Number System Conversion and Information EncodingC++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Join the fan group to receive a free electronic version of the Black Cat teaching plan.

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Number System Conversion

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding
#include<bits/stdc++.h>
using namespace std;

int main(){
 int a = 0b101010;   // Binary
 int b = 03657;      // Octal
 int c = 99;         // Decimal
 int d = 0x5AF3;     // Hexadecimal
 
 cout << a << " " << b << " " << c << " " << d;
 return 0;
}

// Output: 42 1967 99 23283

Binary

Computers only recognize binary numbers: digits, text, images, videos, etc., are all stored as 01 in computers.

To convert binary to decimal, sum according to the weight.

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Example: (June 2023) The data stored in computers is in binary form. Therefore, when writing programs in C++, rewriting all decimal numbers as their binary equivalents will improve program efficiency.

  • True
  • False

Answer:False

Example: (Sample question) For a decimal number 37, which of the following is its binary representation ( ).

A. 10101

B. 100101

C. 101001

D. 1000101

Answer:B

Example: (September 2023) The binary number 101.101 is 5.005 in decimal.

  • True
  • False

Answer:False

Example: (June 2023) The binary number 11.01 is ( ) in decimal.

A. 3.01

B. 3.05

C. 3.125

D. 3.25

Answer:D

Example: The decimal fraction 13.375 corresponds to the binary number ( ).

A. 1101.011

B. 1011.011

C. 1101.101

D. 1010.01

Answer:A

Octal and Hexadecimal

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Example: (June 2023) In C++, the expression (0xf == 015) evaluates to true.

  • True
  • False

Answer:False

Example: (Sample question) Which of the following statements about hexadecimal is correct ( ).

A. Uses 0-9 and A-F

B. Uses 0-9 and A-E

C. Uses 1-9 and A-F

D. Uses 1-9 and A-E

Answer:A

Example: (September 2023) Given that the ASCII code for the uppercase character ‘A’ in hexadecimal is 0x41, what is the hexadecimal representation of the ASCII code for the character ‘L’ ( ).

A. 4A

B. 4B

C. 4C

D. 52

Answer:C

Example: (June 2023) Given that the ASCII code for the uppercase character ‘A’ in hexadecimal is 0x41, what is the hexadecimal representation of the ASCII code for the character ‘F’ ( ).

A. 46

B. 47

C. 48

D. 49

Answer:A

Example: (June 2023) Which of the following statements about number systems is incorrect ( ).

A. The binary representation of positive integers only contains 0 and 1.

B. 10 is not an integer power of 2, so decimal numbers cannot be converted to binary.

C. When converting from binary to octal, it is easy to convert every 3 binary digits to the corresponding octal digit from low to high.

D. When converting from binary to hexadecimal, it is easy to convert every 4 binary digits to the corresponding hexadecimal digit from low to high.

Answer:B

Example: (September 2023) Which of the following statements about number systems is correct ( ).

A. Only decimal and binary can be used to represent fractions; octal and hexadecimal cannot.

B. Common number systems include binary, octal, decimal, and hexadecimal; other systems are rarely used in daily life.

C. For any positive integer, its binary representation will not be shorter than its decimal representation.

D. The maximum digit that can appear in the octal representation of positive integers is 8.

Answer:C

Example: The binary number 1101111 converted to hexadecimal is ( ).

A. 157

B. 111

C. 6f

D. 3f

Answer:C

Example: The octal number equivalent to the binary fraction 0.1 is ( ).

A. 0.8

B. 0.4

C. 0.2

D. 0.1

Answer:B

Example: Among the following number systems, the one that has a different value from the other three is ( ).

A. 10111(2)

B. 23(10)

C. 27(8)

D. 18(16)

Answer:D

Information Encoding

“In a distant kingdom of computers, there lived a mathematical genius named ‘Adder’ who could only perform addition. One day, the king assigned him a task: to calculate how much. The Adder scratched his head and said: ‘Your Majesty, I can only add!’ So he transformed the problem into , but when he calculated using original code, he got this incorrect answer! The entire kingdom fell into a mathematical crisis…”

Smart students might wonder, what is original code?

The true value that humans intuitively understand refers to the actual value of a number in mathematics, including its sign and magnitude. For example, and are both true values. The form in which computers store and calculate numbers is: binary, but computers cannot directly represent signs ( or ), so a coding method is needed to represent positive and negative numbers, which led to the birth of original code.

Original Code: The highest bit is usually used to represent the sign ( for positive, for negative), and the remaining bits represent the value. For example, in an eight-bit binary number:

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

“Why did such an error occur? How should the kingdom of computers solve this problem?”

Adding positive numbers is not a problem, but adding negative numbers causes issues: the addition of negative numbers only considers the increase in absolute value without considering the characteristics of negative numbers. Since the absolute values of negative numbers are opposite, addition of negative numbers in original code becomes a challenge.

At this time, another brilliant mathematician appeared in the kingdom of computers, who invented complement code based on original code, which can successfully solve the problem of adding negative numbers!

The definition of complement code is as follows:

  1. The complement of a positive number is the same as its original code;
  2. The complement of a negative number is obtained by inverting the bits of the positive number, keeping the sign bit as ;
C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

Original code is intuitive but can lead to errors in calculations.

Complement code solves some problems but creates .

Two’s complement perfectly solves all problems and becomes the coding used in computers.

Example: (June 2023) There are only three types of data encoding: original code, complement code, and two’s complement.

  • True
  • False

Answer:False

Example: (Sample question) In binary data encoding, the complement of a negative number is obtained by inverting the original code bit by bit and adding 1.

  • True
  • False

Answer:True

Example: (September 2023) Which of the following statements about the original code, complement code, and two’s complement of negative numbers is correct ( ).

A. Original code and complement code are bitwise inverses (excluding the sign bit), and two’s complement is the complement code plus 1.

B. Original code and complement code are bitwise inverses (excluding the sign bit), and two’s complement is the original code plus 1.

C. Complement code and two’s complement are bitwise inverses (excluding the sign bit), and original code is the complement code plus 1.

D. Two’s complement and original code are bitwise inverses (excluding the sign bit), and complement code is the two’s complement plus 1.

Answer:A

Example: In 8-bit two’s complement, 10101011 represents the decimal number ( ).

A. 43

B. -85

C. -43

D. -84

Answer:B

Example: The two’s complement of an 8-bit integer is 11111001, what is its original code ( ).

A. 00000111

B. 01111001

C. 11111001

D. 10000111

Answer:D

Lifetime VIP Long press to recognize the QR code

Those who have purchased the course can contact customer service to refund the price difference.

VIP includes the complete set of online platform courses, continuously updated exam competition questions, and new courses will automatically be included without additional payment.

C++ GESP Level 3 Key Points: Number System Conversion and Information EncodingC++ GESP Level 3 Key Points: Number System Conversion and Information EncodingC++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

WeChat|blackcat1995com

Public Account|Learn Programming with Black Cat

Related Recommendations

C++ GESP Level 3 Key Points: Number System Conversion and Information Encoding

If you like it, please“share” it~

Leave a Comment