Computers are sometimes unable to accurately represent real-world numbers , one is because computers indicate that the bit used by the numbers is limited, and another reason is that some numbers themselves cannot be stored in computer (binary) notation, such as 0.1, 0.2, and so on.
In object-oriented programming languages, C + + itself is based on C, which uses exactly the same numeric representations and operations, and the C standard is designed to allow multiple implementations, while the Java standard is very precise in terms of the format and coding of the data.
2.1 Information Storage
bytes are the smallest addressable memory units, rather than accessing individual bits in memory. The machine-level program treats the memory as a very large byte array, the virtual memory (as mentioned in chapter I, the virtual memory includes memory and I/O devices, and I/O devices are called files at the level of abstraction). Each byte of memory has an address, collectively known as the virtual address space.
2.1.16 Binary notation
In the C language, numbers beginning with 0x and 0X are hexadecimal digits. The character ' A ' ~ ' F ' can be uppercase or lowercase, or even mixed in case (as in C, Java also starts with 0x to represent hexadecimal values, while octal numbers start with 0 (though it is easier to confuse), the binary number starts with 0b. -Source: Java Core Technology Vol. 1 (Chinese 9th edition), page 33rd to 34th).
Conversion technique between binary number and hexadecimal number: If X is a non-negative integer n power of 2, then the binary representation of X is 1 followed by n 0. A hexadecimal number is followed by how many 0, the converted binary number after the number of 0 is equal to the former 4 times times. In summary, when n can be expressed as I + 4j (where 0≤i≤3), we can write X as the beginning of the hexadecimal number is 1 (i = 0), 2 (i = 1), 4 (i = 2), 8 (i = 3), followed by J hexadecimal 0.
The parsing of this technique:
I. We all know that 4 bits can represent a hexadecimal digit, i.e. (0000) 2 corresponds (0) 16 to (1111) 2 corresponds to (F) 16, if (1111) 2 is added 1, i.e. (10000) 2, the binary number cannot be represented by a hexadecimal digit.
Ii. According to our experience, if the leftmost side of a binary number is 1 and the right of 1 is n 0, then this number is converted to decimal number 2n, and vice versa.
Iii. binary conversion to 16 binary is the entire binary number from right to left group, 4 bits for a group, from the previous narrative that each group corresponds to a hexadecimal digit, so a 1 for the beginning, followed by n 0 of the binary number, when converted to hexadecimal number, it has a J = N/4 0. As for the final (0001) 2 (with i = 0 0), (0010) 2 (with i = one 0), (0100) 2 (with i = 2 0), (1000) 2 (with i = 3 0), convert it to hexadecimal.
The conversion between binary, octal, decimal, and hexadecimal in Java is done through the valueof, parseint, and ToString methods under Java.lang.Integer. --"Java Core Technology Volume 1 (Original book 9th edition)" 189th page
2.1.3 Data size
The word length is the nominal size of the integer and pointer data.
The number of bytes allocated for a data type in the C language depends on the machine and the compiler. Therefore, programs written in C can be limited in portability.
2.1.4 Addressing and byte order
When a program object spans multiple bytes, we need to consider two questions: what is the address of this object and how these bytes are arranged in memory. In general, multibyte objects are arranged in contiguous bytes in memory, with the address of the object being the smallest address in the bytes used.
Be aware of the concept of the small-end method and the big-endian method on page 26th of this book.
Because no matter what type of machine compiles the same results for the program, so for most programmers, the byte order used by their machines is completely invisible. But byte order can also be a problem in some cases. The first is that when transmitting binary data over a network between different types of machines, the receiver obtains a byte order that is diametrically opposed to the sender. In this regard, in the writing of the network application should abide by the network standard about byte order, the receiver can convert the data that conforms to the network standard to its internal representation, as a compromise scheme.
The second case in relation to byte order is that the byte order that we use to check the sequence of bytes in a machine-level program is also important. When reading a machine-level program generated by a small-end method, it is often necessary to read the bytes in reverse order, so that the resulting number is the most significant bit on the left and the least significant bit on the right side of the number we are writing.
Notice the description of the forced type conversion and the notes I made on page 33rd to 35th of this book.
The string in the C language is encoded as a null character '/0 ' as a string termination flag, while Java does not have this argument.
The same result can be obtained on any system that uses ASCII code as a character code, regardless of byte order and word size, so the text data has a stronger platform independence than binary data.
In C, for example, when a C function is compiled on a different type of machine, a different binary machine code is generated. Binary code is incompatible because different machine types use different and incompatible instructions and encodings , even if the same process has different encoding rules on different operating systems.
About truncation problems in integer operations:
Whether it is unsigned number or signed number, the addition of multiplication basic operations between them is based on the establishment of a bit-level representation, the final result no matter overflow, in-place mode will maintain the original number of bits.
Integer multiplication:
Integer multiplication instructions consume more clock cycles, while other integer operations (such as addition, subtraction, bit-level operations, and shifts) require only 1 clock cycles. In order to optimize it, the compiler replaces the multiplication of the constant factor with a combination of shift, addition, and subtraction.
Of course, the choice of using a combination of shift, addition, and subtraction, or a multiplication instruction, depends on the relative speed of these instructions, which are related to the height of the machine.
Integer Division:
Integer division is slower than integer multiplication. The power of dividing by 2 can also be implemented with shift-right shift. The unsigned and complement numbers use logical shifts and arithmetic shifts respectively to achieve the goal.
Two positive integers (either unsigned or signed), the result is a positive integer (the fractional part is rounded off). This is related to the representation and displacement of the bit level.
When a negative integer (signed number) participates, the shift causes rounding down instead of rounding to 0 (which is contrary to our subjective consciousness). At this point, the divisor of less than 0 can be biased (plus the divisor minus 1) before the normal right shift, so you can get the correct result.
The right shift of integers in integer division, whether unsigned or signed, can produce results that match our subjective intention, while the right shift of negative integers (signed numbers) does not produce the correct result (see Section 2.3.7 in the book):
- A positive integer that, after the right shift, may move the non-0 bits of the original rank to the back of the decimal point, removing some of the non-0 bits by moving right (that is, dividing by 2k), while in the computer's integer operation it is possible to display only the bit pattern to the left of the decimal point. But in Nature , non-zero (that is, the non-0-bit above) no matter how it is removed, the result will not be zero. such as 1 divided by 2, 1 divided by 22, 1 divided by 24, 1 divided by 216 ..., the result is not zero, but the precision can be retained to how much. That is, after the positive integer is removed, its result may also have a nonzero number after the decimal point, but because of the nature of the integer operation in the computer, these non-zero are erased, leaving only the integer to the left of the decimal point, and the result is only smaller than the result of the division operation we learned in primary school. But it conforms to the understanding of Computer integer division in our host consciousness, which is rounded to 0.
- The right shift of a negative integer is the same as above. Move right to erase the non-0-bit to the right of the decimal points of the result. These non-0 bits are actually positive, but because they are erased, the result subtracts the values of the non-0 bits represented by the original negative result, and the final result is rounded down rather than rounded to 0.
Floating point number:
Standard for representing floating-point numbers and their operations: IEEE Standard 754.
Floating-point numbers are normalized, non-normalized, infinite, and Nan four cases.
The presentation and processing of information in the second chapter of the learning process of the Deep learning computer system (C language)