In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "C language binary conversion, integer and decimal memory storage model is what", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C language binary conversion, integer and decimal memory storage model is what" it!
What is binary?
Binary, that is, carry counting system, is an artificially defined counting method with carry. For any system-N system, it means that the number in each position is calculated with one digit per N.
I believe everyone knows how to count, for example, 0 12 3 4 5 5 6 7 8 9 10 11 12 13. When counting, a certain digit is carried forward when the number is over 10. This kind of carry system for every decimal is called the decimal system.
However, in daily life, there is more than this carry system, such as an hour has 60 minutes, a minute has 60 seconds, full 60 into one, this is the hexadecimal system.
In addition to decimal, there are binary, octal and hexadecimal systems commonly used in computers.
Binary system
Composition: 0 1
Rule: every two into one
Representation: the binary number 1000010 can be written as (1000010) 2 or 1000010B
Octal system
Composition: 0 1 2 3 4 5 6 7
Rule: every eight into one
Representation: octal number 520 can be written as (520) 8 or as 520O
hexadecimal
Composition: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Rule: every sixteen into one
Representation: 520 in hexadecimal can be written as (520) 16 or 520H
Why are there so many binary representations in computers?
Convenience: there are only two digits 0 and 1 in the binary number, and one digit can be represented by components with two different stable states.
Simple: the binary number operation is simple, which greatly simplifies the structure of the operation unit in the calculation.
True or false: binary is naturally compatible with logical operations.
Disadvantages: there is an inconvenience in the daily use of binary counting, that is, the number of digits is often very long, and it is inconvenient to read and write. For example, writing the decimal 100000D into binary is 110000110100000B, so we actually use hexadecimal in the computer field. When a binary number is converted to a hexadecimal number, the length is reduced to about 1/4, and writing 100000 of the decimal into octal is 303240. One hexadecimal digit can represent four binary digits. In this way, a decimal 100000 written in hexadecimal is 186A0.
Storage unit
The programs we usually use, such as Windows operating system, typing software, game software and so on. Generally installed on the hard disk and other external memory, but only this can not use its functions, they must be called into memory to run in order to really use its functions.
Because the read and write speed of memory is very fast compared to external memory, but memory is the place to store programs and data temporarily. When we use WPS to process a document, when you type a character on the keyboard, it is stored in memory. When you choose to save the disk, the data in memory will be saved to the hard (magnetic) disk.
Memory is made up of countless transistors (understood as light bulbs), and one transistor acts as an one-bit (bit) memory. Each transistor can store a binary 0 or 1, and bits are usually called bits.
Bit (bit): the smallest unit of computer storage
Byte: the smallest unit of data representation
A byte is usually 8 bits long 1byte = 8 bit
Kilobytes (KB):
1KB = 1024byte
Why 1024 instead of 1000? The tenth power of two happens to be 1024, that's what it means.
Conversion units of more than one byte are all 1024. Only one byte equals eight bits is different.
Think about it: why is the capacity of the hard disk different from the actual capacity?
The 256g hard drive bought actually has only 238.4G. Let's convert it together:
Hard disk manufacturer decimal calculation: 256G = 256000MB = 256000000KB = 256000000000Byte conversion operating system binary calculation in 1000 units: 256g = 262144MB = 268435456KB = 274877906944Byte conversion in 1024 units so 256g actual capacity: 256000000
000Byte/1024MB/1024MB/1024MB = 238.4G
So, buying a 256g hard drive actually costs only 238.4G, and the larger the capacity, the greater the gap.
Binary conversion decimal to other decimal systems: short division
Take the decimal number 520 as an example, which is converted to binary, octal and hexadecimal, respectively. The conversion process is as follows:
Other decimal conversion: bit weight addition
Take the binary, octal and hexadecimal of 520D as an example.
First of all, you need to number the other bases from right to left, 0 1 2 3 4 5.
Then, the number of each digit is calculated by this formula [numerical * cardinality ^ number], and then the results are added to get the conversion result.
Binary 10 0000 1000 to decimal
98 7654 3210 No. 10 0000 1000 B 1 * 2 ^ 9 + 0 + 1 * 2 ^ 3 = 512 + 0 + 8 = 520D
Octal 1010 to decimal
3210 number 1010 O 1 * 8 ^ 3 + 0 + 1 * 8 ^ 0 = 520 + 8 = 520D
Hexadecimal 208 to decimal
210 number 208 H 2 * 16 ^ 2 + 0 + 8 * 16 ^ 0 = 2 "256 + 8 = 520 H octal, hexadecimal and binary conversion: split bit
Octal and binary
An octal number can be split into three binary numbers, and three binary numbers can be combined into one octal number.
/ / binary to octal 001 000 001 000 B 101 00 O / octal to binary 1 3 1 4 5 20 O 001 011 001 100 101 010 000 B
Hexadecimal and binary
An octal number can be split into four binary numbers, and four binary numbers can be combined into one octal number.
/ / binary to hexadecimal 0010 0000 1000 B 20 8 H / / hexadecimal to binary 1 3 1 4 5 20 H 0001 0011 0001 0100 0101 0010 0000 B why can it be split in this way?
The range of three-digit binary numbers is [0-8)-> 2 ^ 3 is just right for octal.
The range of four-digit binary numbers is [0-16)-> 2 ^ 4 is just right for hexadecimal.
The storage mode of integers is one, the true value of the number of machines and the number of machines is 1, the number of machines
A binary representation of a number in a computer, called the machine number of this number. The number of machines is signed and the symbols are stored in the highest bit of the number of machines used in the computer, with a positive number of 0 and a negative number of 1.
For example, the number + 3 in the decimal system, the computer word length is 8 bits, the conversion to binary is 0000 0011, if it is-3, it is 100 00011.
So, 0000 0011 and 1000 0011 here are the number of machines.
2, the true value of the number of machines
Because the first bit is a symbolic bit, the formal value of the machine number is not equal to the real value.
For example, the signed number above 1000 0011, the highest bit 1 represents negative, and its real value is-3, rather than the formal value of 1000 0011 converted to decimal. Therefore, for the sake of difference, the real value corresponding to the number of machines with symbols is called the true value of the number of machines.
Example: true value of 0000 0001 = + 0001 0001 = + 1 0001 0001 =-1
Second, original code, inverse code, complement code
Let's first understand the concepts of source code, inverse code and complement code. For a number, the computer should use a certain coding method to store, the original code, inverse code, complement code is the machine to store a specific number of coding methods.
1, original code
The original code is the number of machines, that is, the first bit is used to represent the symbol, and the remaining bits represent the value. For example, if it is an 8-bit binary:
[+ 1] original = 0000 0001
[- 1] original = 1000 0001
The first bit is the symbol bit, because the first bit is the symbol bit, so the value range of the 8-bit binary number is: (that is, the first bit does not represent a value, only a positive or negative value.)
[1111 1111, 0111 1111] that is [- 127127]
The original code is the easiest way for the human brain to understand and calculate.
2, inverse code
The inverse of a positive number is itself.
The inverse code of a negative number is based on its original code, the symbol bits remain unchanged, and the other bits are inverted.
[+ 1] = [0000 0001] original = [0000 0001] inverse
[- 1] = [1000 0001] original = [1111 1110] inverse
It can be seen that if an inverse code represents a negative number, the human brain cannot directly see its value. It is usually converted to the original code and then calculated.
3, complement
The complement of a positive number is itself.
The complement of a negative number is based on its original code, the symbol bit is unchanged, the rest of you are reversed, and finally + 1. (that is, + 1 on the basis of inverse code)
[+ 1] = [0000 0001] original = [0000 0001] inverse = [0000 0001] complement
[- 1] = [1000 0001] original = [1111 1110] inverse = [1111 1111] complement
For negative numbers, the complement representation is also impossible for the human brain to directly see its value. Usually, it also needs to be converted to the original code and then its value is calculated.
Third, why should we use original code, inverse code and complement code?
The human brain can know that the first bit is the symbol bit, and we will choose to add and subtract the true value region according to the symbol bit in the calculation. (the concept of true value is at the beginning of this article.) but for computers, addition and subtraction multipliers are already the most basic operation, and to design them as simply as possible, computer identification of "symbol bits" will obviously make the basic circuit design of the computer very complicated!
So people came up with a way to involve symbolic bits in the operation. We know that subtracting a positive number according to the algorithm is equal to adding a negative number, that is, 1-1 = 1 + (- 1) = 0, so the machine can only add but not subtract, so the design of computer operations is easier.
Let's take calculating the decimal expression: 1-1 = 0 as an example.
First of all, let's look at the original code:
1-1 = 1 + (- 1) = [0000 0001] original + [1000 0001] original = [1000 0010] original =-2
If you use the original code and let the symbol bits also participate in the calculation, it is obvious that the result is incorrect for subtraction. This is why the original code is not used inside the computer to represent a number.
In order to solve the problem of subtraction of the original code, there is an inverse code:
1-1 = 1 + (- 1) = [0000 0001] original + [1000 0001] original = [0000 0001] anti + [1111 1110] inverse = [1111 1111] inverse = [1000000] original =-0
It is found that the true value of the result is correct when the inverse code is used to calculate the subtraction. In fact, the only problem is the special value of "0". Although people understand that + 0 and-0 are the same, the sign of 0 does not make any sense, and there will be two codes [0000 0000] and [1000 0000] that represent 0.
So the emergence of complement solves the symbol problem of 0 and two coding problems of 0:
1-1 = 1 + (- 1) = [0000 0001] original + [1000 0001] original = [0000 0001] complement + [1111 1111] complement = [1 0000 0000] complement = [0000000] complement = [0000000] Note: carry 1 is not in the computer word length.
This 0 is represented by [0000 0000], while the previously problematic-0 no longer exists. And it can be expressed as [1000 0000]. The origin of-128 Muri 128 is as follows:
(- 1) + (- 277) = [1000 0001] original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] complement
The result of-1-127should be-128. in the result of complement operation, [1000] complement is-128, but note that because it is actually represented by the complement of the previous-0, there is no original and inverse representation. (the complement of-128represents [1000 0000] complement, and the calculated original code is [0000 0000] yuan, which is incorrect)
The use of complement not only fixes the symbol of 0 and the problem of two encodings, but also represents a minimum number. This is why 8-bit binaries have a range of [- 127, + 127] in source or inverse codes, and [- 128,127] in complements.
The storage of integers is to convert decimal integers into their corresponding complements and store them.
The way decimals are stored
Nowadays, floating point data in computers are stored in memory in accordance with the IEEE754/854 standard and stored in binary scientific counting.
For floating point data, there are two kinds of precision stored in the computer, namely, single-precision and double-precision, single-precision is 32-bit, double-precision is 64-bit.
The symbol Spurs 0 is positive and 1 is negative
Mantissa M: the part after the decimal point.
Index E: the order code, which indicates the position of the decimal point in the data
In order to make the exponential representation positive and negative, the deviation code is introduced, the value of float is 127 and double is 1024.
Decimal to binary decimal
Convert the integer part to binary first.
Then convert the decimal part to binary (multiply the decimal part by 2, take out the result integer each time, and then continue to multiply by 2 with the remaining decimal part until the decimal part is zero, or until the required precision is reached)
Take float f = 5.25 as an example
Integer part: 5-> 101
Decimal part: 0.25-> 0.01
0.25 * 2 = 0.5-0 0.5 * 2 = 1.0-1 value from top to bottom: 0.01
Final result: 101.01 = 1.0101 * 2 ^ 2
It can be seen that the actual value of the index is 2, and the binary with the deviation code 127J2 + 127127 = 129129 is 10000001B, so it is not difficult to obtain that 8.25 is stored in memory as follows:
If you use this value as an integer, it will be a large number, 1084751872.
It is easy to convert the value in this memory to decimal:
/ / 1, first judge the positive and negative + / / 2 represented by S, calculate the actual index 1000 0001 = 129129127 = 2 / 3, write the binary decimal form 1.0101 * 2 ^ 2 = 101.01 / 4 according to M, and start with the decimal point as the limit. 0 1 B 1 * 2 ^ 2 + 0 + 1 * 2 ^ 0 + 0 * 2 ^ (- 1) + 1 * 2 ^ (- 2) = 4 + 1 + 0.25 = 5.25 Note:
In binary, the first significant number must be "1", so this "1" is not stored.
A floating point number cannot accurately represent all numbers in its range.
The number that can be accurately expressed is not uniformly distributed, and the closer it is to 0, the denser it is.
At this point, I believe you have a deeper understanding of "C language binary conversion, integer and decimal memory storage model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.