Explain the binary number in the form of data stored inside the computer

Explain in detail the form of data stored in the computer-binary numbers
Preface
To form a general impression of the operating mechanism of the program, it is necessary to understand the form of information (data) in the computer and the method of operation of. In
programs written in high-level languages ​​such as C and Java, information such as numerical values, character strings, and images are expressed in the form of binary numerical values ​​inside the computer. In other words, as long as you have mastered the method of using binary numbers to represent information and its operation mechanism, you will naturally be able to understand the operating mechanism of the program. So, why should the information processed by the computer be represented by binary numbers?

1. Reasons for using binary numbers to express computer information The
inside of the computer is composed of electronic components such as IC (Integrated Circuit). CPU (microprocessor) and memory are also a type of IC. IC has several different shapes, some are like a black centipede, with several or even hundreds of pins on both sides of it; others are like a needle plate for flower arrangement, the pins are arranged side by side inside the IC. == All pins of the IC have only two states of DC voltage 0V or 5VB ==. In other words, one pin of the IC can only represent two states. This characteristic of IC determines that the information data of the computer can only be processed with binary numbers.

The smallest unit of computer processing information-bit, is equivalent to one bit in binary. The English bit is the abbreviation of binary digit.
An 8-bit binary number is called a byte.
Bit is the smallest unit, and byte is the basic unit of == (information) ==.
When processing data in byte units, if the number is less than the number of bytes of the stored data (the number of digits in the binary number), then the upper bits are filled with 0. For example, the 6-bit binary number 100111 is 00100111 when represented by 8 bits (=1 byte), and 0000000000100111 when represented by 16 bits (= 2 bytes).
2. What is a binary number
Binary is a number system widely used in computing technology. Binary data is a number represented by two digits, 0 and 1. Its base is 2, the carry rule is "every two enters one", and the borrow rule is "borrow one and become two" to
convert the value of a binary number into a decimal number. You only need to change the value and weight of each digit Multiply,
and then add the results of the multiplication together. For example, if 00100111 is converted to a decimal number, 39

Why is it calculated like this? In fact, the ideas of binary and decimal systems are the same. For example, the decimal number 39=(3 x 101) +(9 x 100), where 10 is the base in the power function, and 10 and
1, which are multiplied by the value of each digit , are the digits. right. Binary and decimal calculation methods are just different bases.
3. The relationship between shift operation and multiplication and division operation
Shift operation refers to the operation of shifting each digit of a binary value left and right (shift = shift). There are two types of shifting: left shift (to the high bit direction) and right shift (to the low bit direction). In one operation, multiple digit shift operations can be performed.
The << operator means left shift, and the >> operator is used when shifting right. The left side of the << operator and >> operator is the value to be shifted, and the right side indicates the number of bits to be shifted.
After
shifting to the left, the vacant low bit needs to be filled with 0. The shift operation causes the most significant or least significant digit to overflow, just discard it directly. For example, the decimal number 39 is 00100111 in 8-bit binary representation, 10011100 after shifting to the left by two bits, and then converted to a decimal number is 156. 

Shift operations can also replace multiplication and division by shifting digits. After shifting two bits to the left, the value becomes
4 times the original value . After the decimal number is shifted to the left, it will become 10 times, 100 times, 1000 times the original... Similarly, after the binary number is shifted to the left, it will become 2 times, 4 times, 8 times the original... On the contrary, after the binary number is shifted to the right It will become the original 1/2, 1/4, 1/8...
Four. When a negative value is expressed in a "complement"
binary number that represents a negative number, the highest bit is generally used as a sign, so we set this highest The bit is called the sign bit. When the sign bit is 0, it means a positive number, and when the sign bit is 1, it means a negative number.
So what does -1 look like if it is represented by an 8-bit binary number? Many people may think that "1 is 00000001 in binary number, so -1 is 10000001", but this answer is wrong, and the correct answer is 11111111.
When a computer is doing a subtraction operation, it is actually doing an addition operation internally. Addition is used to realize subtraction. For this reason, it is necessary to use "binary's complement" when expressing negative numbers. Complement is the use of positive numbers to represent negative numbers.
How to get the complement? It is only necessary to invert all the values ​​of the digits of the binary number, and then add 1
to the result to express it with an 8-bit binary number-when 1, only 1 is required, which is the complement of 00000001. Specifically, it is to reverse the 0 of each digit to 1, and 1 to 0, and then add 1 to the inverted result, and finally it is converted to 11111111

So, for a complement, how do you know the negative value it represents? At this time, we can take advantage of the property that negatives become positives. The complement represents a negative number, then the complement of the complement represents a positive number, and the negative of this positive number is the value of the complement. For example, 11111110, the highest bit is 1, so it represents a negative number, and 11111110 is inverted and 1 is added to 00000010. This is a decimal number of 2. Therefore, 11111110 means -2.
5. The difference between logical shift right and arithmetic shift right
When the value of a binary number represents a graphic mode instead of a numerical value, == needs to be filled with 0 == at the highest bit after shifting. Similar to the effect of a neon light scrolling to the right. This is called logical shift right

When performing operations on binary numbers as signed values, the highest bit should be filled with the value of the sign bit (0 or 1) before the shift after shifting. This is called arithmetic shift right. That is to say, if the value is a negative value represented by a complement, then after shifting to the right, add 1 to the highest digit that is vacated, and then the numerical operations of 1/2, 1/4, 1/8, etc. can be correctly implemented. If it is a positive number, just add 0 to the highest bit.

Only when shifting to the right, it is necessary to distinguish between logical displacement and arithmetic displacement. When shifting to the left, whether it is in graphics mode (logical shift to the left) or multiplication operation (arithmetic shift to the left), you only need to add 0 to the vacant low bit.

Sign extension refers to converting it into 16-bit and 32-bit binary numbers while keeping the value unchanged.

Sign extension method: whether it is a positive number or a negative number represented by a complement, only the high bit is filled with the value of the sign bit (0 or 1). For example, the positive 8-bit binary number of 01111111 is converted into a 16-bit binary number is 0000000001111111; the negative number represented by the complement of 11111111 is converted into a 16-bit binary number is 1111111111111111

In operations, the term opposite to logic is arithmetic. Let us consider it this way. It is arithmetic to treat the information represented by the binary number as the value of the four arithmetic operations. And like the graph mode, the listing of values ​​as simple 0 and 1 is logic.

Arithmetic operations refer to the four arithmetic operations of addition, subtraction, multiplication and division.

Logical operations refer to operations that process the 0 and 1 of each digit of a binary number, including logical NOT (NOT operation), logical AND (AND operation), logical OR (OR operation) and logical exclusive OR (XOR operation A) Four kinds.

Logical negation refers to the inversion operation where 0 becomes 1 and 1 becomes 0.

Logical AND refers to the operation in which the operation result is 1 when "both are 1", and the operation result is 0 in other cases

Logical OR refers to an operation in which "at least one of them is 1", the result of the operation is 1, and in other cases, the result of the operation is 0.

Exclusive logical OR refers to operations that exclude the same value. "The two values ​​are different", that is, when "one is 1 and the other is 0", the result of the operation is 1, and the result is 0 in other cases.

The logical truth table is as follows:

Guess you like

Origin blog.csdn.net/feng8403000/article/details/114905170