binary
As we all know, the bottom layer of the computer uses binary data for data flow transmission, so why use binary to represent the computer? In other words, what are binary numbers? In a further step, how to add, subtract, multiply and divide using binary? How do binary numbers represent negative numbers? This article will tell you all about it.
Why binary
As we all know, the computer is composed of IC electronic components, which CPU and memory is also a kind of IC electronic components, CPU and memory figure is as follows
CPU and memory use IC electronics as basic units. IC electronics come in different shapes, but their internal components are called pins. Some people say that both CPU and memory are internal vlsI. In fact, IC is Integrated Circuit.
IC components on both sides of the square block is the pin, all the pins of IC, only two kinds of voltage: 0V and 5V, this characteristic of IC, also determines the information processing of the computer can only be expressed with 0 and 1, that is, binary processing. A pin can represent a 0 or a 1, so the binary representation becomes 0, 1, 10, 11, 100, 101, etc. Although binary numbers are not specifically designed for pins, they are very consistent with the characteristics of IC pins.
The smallest integrated unit of a computer is a bit, also known as a bit (bit). The bits of a binary number are generally 8, 16, 32 and 64 bits, which is a multiple of 8. Why do we have to relate to 8? Because in computers, 8-bit binary numbers are called a byte, and a byte has 8 bits, which is made up of 8 bits.
Why is one byte equal to eight bits? Since 8 bits cover all character encodings, this is all you need to remember.
The byte is the most basic unit of measurement and the bit is the smallest.
When processing data in bytes, if the number is less than the number of bytes stored (= the number of bits in binary), then the high level is filled with 0. The high level is the same as the mathematical representation of numbers, with ** to the left indicating the high level and the right indicating the low level. ** For example, the six-digit binary number is 100111, with only six bits, and the highest part needs to be filled with 0. After filling, 00100111 takes one byte. If it is expressed in 16 bits, it is 0000 0000 00100111 takes two bytes.
When we talk about 32-bit and 64-bit computers, we usually refer to the processing bits. 32-bit can represent 4 bytes at a time, and 64-bit can represent 8 bytes at a time.
The logical operations that we normally use in software development, such as decimal numbers, will also be converted to binary number processing by computers. With binary numbers, computers do not distinguish between images, audio files, or numbers, which are all combinations of data.
What is a binary number
So what is a binary number? To illustrate this problem, let’s first convert the number 00100111 to a decimal number. Let’s see, the binary number to a decimal number, directly convert the value of each position * bit weight, so let’s convert the value above
In other words, the binary number 00100111 is 39 in decimal, which is not written as 3 and 9, but 3 * 10 + 9 * 1, where 10, 1 is the bit weight, and so on, In the example above, the bit weights from highest to lowest are 7, 6, 5, 4, 3, 2, 1, 0. This bit weight is also called the power, so the highest bit is 2 to the seventh, 2 to the sixth and so on. Binary numbers are always evaluated to base 2, which is the base, so the base of a decimal number is 10. In any case the value of the bit weight is the number of bits -1, then the first bit weight is 1-1 = 0, the second bit weight is 2-1 = 1, and so on.
So the binary number we are talking about is actually a number represented by 0 and 1. Its base is 2, and its value is the sum of the bits * weight of each number. We generally refer to the decimal number, so its value is 3 * 10 + 9 * 1 = 39.
Shift operation and multiplication and division
Now that we know about binary, let’s look at binary operations. Just like decimal numbers, addition, subtraction, multiplication and division work for binary numbers, as long as you pay attention to the round of 2. The operation of binary numbers is also the unique operation of computer programs, so it is necessary to understand the operation of binary.
First, we will introduce the shift operation. The shift operation refers to the operation of moving the elements of each position of the binary value to the left and right, as shown in the figure below
In the example above, we convert the decimal number 39 to the binary number 0010 0111, then shift it to the left by << one byte, which becomes 0100 1110, then convert the binary number to the decimal number 78. The number 78 in decimal is twice the number 39 in decimal. We’re moving 0010, 0111 two Spaces to the left, 1001, 1100, and we’re getting 156, which is four times larger!
So you can conclude that a shift to the left is equivalent to an enlargement operation. What about a shift to the right? It’s supposed to be 1/2 and 1/4 times smaller, but isn’t 39 two and four times smaller a decimal? How do I write this? Look at the next section
A computer-friendly complement
We didn’t show you the right shift just now, because the high order value that’s left empty after the right shift has two forms, 0 and 1. To tell when to add 0 and when to add 1, you first need to know how to represent negative numbers in binary numbers.
When negative values are represented in binary numbers, the highest bit is usually used as a sign, so we use this highest bit as a sign bit. A sign bit 0 represents a positive number and a sign bit 1 represents a negative number. So what’s minus one in binary terms? Many people might think that since the binary number of 1 is 0000 0001, and the highest bit is the sign bit, the correct representation of -1 should be 1000 0001, but is that really the right answer?
There is no subtraction in the computer world, when the computer is doing subtraction is actually doing addition, which is to achieve the subtraction operation with addition. For example, 100 minus 50, when the computer looks at it, it’s 100 plus minus 50, and for that reason, when you write negative numbers, you have to use the binary complement, which is a negative number represented by a positive number.
To get the complement, we need to invert all the bits in binary, and then add one to the result. Remember that, so let’s show it.
To be specific, it is necessary to obtain the binary number of a certain value, and then reverse each bit of the binary number (0 – > 1, 1 – > 0), and finally reverse the number +1, so as to complete the acquisition of the complement.
The acquisition of the complement, although intuitively difficult to understand, but logically very rigorous, for example, let’s take a look at the process of 1-1, we first use the above 1000 0001(it is the complement of 1, do not know the above, just for calculation) to express it
Strangely, 1-1 becomes 130 instead of 0, so it follows that 1000 0001 means -1 is completely wrong.
So what is the correct expression? Actually, we already gave the answer up here, which is 1111, 1111, just to show you that this is true
We can see that 1-1 is actually 1 + (-1). After taking the above inverse + 1, it becomes 1111 1111, and then adding with 1, the result is 1 0000 0000. The overflow occurs and the computer directly ignores the overflow bit. So you flip the highest 1, and it becomes 0000, 0000. That’s 0. That’s right. So 1111, 1111 is minus 1.
So the binary representation of a negative number is to find its complement, and the process of finding the complement is to take the binary bits of the original number and add 1 to the result,
Of course, operations that do not result in zero can also be supplemented to get the correct result. However, it is important to note that when the result is negative, the value of the result is also in the form of a complement, such as 3-5. Let’s look at the parsing process
The result is 1111 1110. As we know, this value definitely represents a negative number, but negative numbers cannot be directly expressed in decimal notation, so we need to take the negative + 1. The result is 2, because 1111 1110 has a high value of 1. So we end up with minus 2.
There are data types in programming languages that can handle negative numbers, and there are data types that can’t. For example, in C language, there are unsigned short types that cannot handle negative numbers, and short types that can handle negative numbers. Both are two-byte variables, and they both have values of 2 to the sixteenth power, but the value range is different. The value range of short types is -32768-32767. The value of unsigned short ranges from 0 to 65536.
If you think about the complement mechanism, you can see why -32768 has one more number than 32767. There are 32768 positive numbers from 0 to 32767, including 0. The highest digit is the negative number of 1, including 32768 from -1 to -32768, excluding 0. Although 0 is neither positive nor negative, it is considered to be positive because of its sign bit.
The difference between an arithmetic right shift and a logical right shift
Now that we know about the complement, let’s revisit the issue of right shift. There are two cases where the highest bit left empty by right shift is 0 and 1. When the value of a binary number represents a graphic pattern rather than a numerical value, the highest bit needs to be filled with a 0 after the shift, similar to the effect of a neon sign panning to the right. This is called a logical right shift.
When a binary number is right-shifted as a signed value, it is necessary to fill the highest bit with the value of the sign bit before the shift (0 or 1). This is called an arithmetic shift to the right. If the value uses the negative value represented by the complement, then the highest bit left empty after the right shift is filled with 1, it can correctly represent the numerical operation of 1/2,1/4,1/8, etc. If it’s positive, you just fill in the empty space with 0.
Let’s look at an example of a right shift. We’re going to move negative 4 two places to the right, and we’re going to look at each of them
As you can see in the figure above, in the case of a logical shift to the right, negative 4 moves two places to the right will become 63, which is obviously not a quarter of it, so you can’t use a logical shift to the right, so in the case of a mathematical shift to the right, negative 4 moves two places to the right will become negative 1, which is obviously a quarter of it, so you use a mathematical shift to the right.
Then we can get a conclusion: when moving to the left, no matter the graph or the value, after the shift, we only need to add 0 to the low position; When you move right, you need to determine whether you move right logically or arithmetically.
Here’s a look at sign extension: Sign extension of data is used to produce a result that doubles the number of digits, but does not change the size of the digit. Some instructions require that the number of digits be operated on, such as the dividend that is longer than the divisor, or the number of digits is longer to reduce errors in the calculation process.
In the case of 8-bit binaries, symbolic extension means converting them to 16-bit and 32-bit binaries without changing their values. Converting 0111 1111 from a positive 8-bit binary to a 16-bit binary easily yields the correct result 0000 0000 0111 1111, but what about a value represented by a complement such as 1111 1111? I’m just going to call it 1111 1111 1111 1111. In other words, regardless of whether the positive number or the negative number represented by the complement, you just need to fill in the 0 and 1 in the high place.
A trick of logical operations
The difference between mastering logic and operations is that treating the information represented by binary numbers as values of four operations is arithmetic, while treating values as simple lists of zeros and ones, like graphics, is logic
The operations that the computer can process can be roughly divided into logical operations and arithmetic operations. Arithmetic operations refer to the four operations of addition, subtraction, multiplication and division. Logical operation refers to the operation of processing the 0 AND 1 of each digit of binary respectively, including ** logical non (NOT operation), logical AND (AND operation), logical OR (OR operation) AND logical XOR operation ** four kinds.
Logic is not
This is the inverse operation of turning a 0 into a 1 and a 1 into a 0Logic and
It means “if both of them are 1, the result is 1, otherwise it’s 0.”Logic or
It means “if at least one of them is a 1, the result is a 1, otherwise it’s a 0.”Xor logic
The result is 1 if one is 1 and the other is 0, and 0 otherwise.
To master logical operations is to get rid of the idea that binary numbers represent numbers. Instead of thinking of binary numbers as numbers, think of them as ON/OFF ON switches.