In-depth research on the storage representation of data in the Machine

Source: Internet
Author: User
Title: in-depth research on the storage representation of data on machines
The original code, the anticode, and the supplementary code are explained in the first chapter of any basic computer knowledge book, but we can only find some simple definitions in the book, I forgot it soon after reading it. When I was writing a program recently, some strange events occurred. After reading the data and carefully testing and thinking, I came to some conclusions.

A numerical value is expressed as a machine number in a computer. A computer can only recognize 0 and 1. Binary is used, while decimal is used in daily life, "As Aristotle has long pointed out, the widespread use of decimal today is only the result of the fact that most of us have 10 fingers in our lives. although in history, finger counting (hexadecimal) has appeared later than two or three hexadecimal counting. "(from <mathematical development history> if you have time, you can see it ~, (Very interesting). In order to facilitate binary conversion, the hexadecimal (2 4) and octal (23) are used. Next we will go to the topic.

Values have positive and negative values, and the computer uses the highest bit of a number to store the symbols (0 is positive, 1 is negative ). this is the original code of the number of machines. assume that the number of digits processed by the machine is 8. that is, the word length is 1 byte, and the original code can indicate the value range is

(-127 ~ -0 + 0 ~ 127) a total of 256.

With the numerical expression method, you can perform arithmetic operations on the logarithm. however, it was soon discovered that the result was correct when the original code with a symbol bit was used for multiplication and division operations, and a problem occurred during addition and subtraction operations, as shown below: Assume the length is 8 bits

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) ORIGINAL + (10000001) original = (10000010) original = (-2) is obviously incorrect.

Because there is no problem in the addition operation of two integers, the problem occurs on a negative number with a symbol bit, A reverse code is generated for all the characters except the symbol bit. the anticode value space is the same and one-to-one correspondence with the original code. the following is the subtraction operation of the anticode:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) + (11111110) = (11111111) = (-0) there is a problem.

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) + (11111101) = (11111110) = (-1) Correct

The problem occurs in (+ 0) and (-0). In people's computing concepts, Zero is not divided into positive and negative. (Indians first mark zero as a mark and put it into the computation. The Indian mathematical and decimal counts with zero numbers contribute a lot to human civilization ).

So the concept of Code complement is introduced. the complement of a negative number is to add one to the anticode, while the positive number remains unchanged. The original positive number is the same as the reverse code. (-0) is replaced by (-128) in the complement code, so the range of the complement code is:

(-128 ~ 0 ~ 127) a total of 256.

Note: (-128) there is no corresponding source code and anticode. The addition and subtraction operations of (-128) = (10000000) are as follows:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) Fill + (11111111) Fill = (00000000) Fill = (0) Correct

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) Fill + (11111110) Fill = (11111111) Fill = (-1) Correct

Therefore, the purpose of code complementing is:

(1) Enable the symbol bit to join the operation with the valid value to simplify the operation rules.

(2) convert the subtraction operation to addition, further simplifying the line design of the calculator in the computer

All these conversions are performed at the bottom layer of the computer, and all the original codes are used in the assembly, C, and other advanced languages we use.

Summary: Bit logical operations change the data storage on the machine. However, when output is formatted in advanced languages, the advanced language determines that the output is still a complement code, which can be converted to the original code form of the number.

Example:
Int x = 3;
X = ~ X;
Printf ("% d", X );
The output result of X is-4.
If: printf ("% x", x); // The output result of X is in the 32-bit machine, it will be fffffc; that is, the binary form
1111,1111, 1111,1111, 1111,1111, 1111,1100.
When the C language is output, add 1 to the complement code to get-4.

Supplement:
(1) The unsigned number and the signed number operations are performed in the machine in the binary complement operation. The output result depends on the output format parameter settings of the advanced language; the number of signed symbols is also involved in the operation. In fact, the operation in the machine uses the complement operation, that is, it does not distinguish between the signed bit and the numeric bit.
(2) format input and output parameters:
For example, the call format of the printf () function is:
Printf ("<formatted string>", <parameter table> );
The formatting string consists of two parts: some are normal characters, which will be output as is; the other part is the formatting rule character, starting with "%", followed by one or several specified characters, used to determine the output content format.
A parameter table is a series of parameters that need to be output. The number of parameters must be the same as the number of output parameters described in the formatted string. The parameters are separated by commas (,) and the order is one to one, otherwise, unexpected errors may occur.
When there are too many threads, there are too many threads, too many threads.
Symbol function
--------------------------
% D decimal signed integer
% U decimal unsigned integer
% F floating point number
% S string
% C single character
% P pointer Value
% E exponential floating point number
% X, % x unsigned integer in hexadecimal format
% 0 unsigned integer in octal format
% G automatically selects the appropriate notation
When there are too many threads, there are too many threads, too many threads.
Note:
(1). You can insert a number between "%" and a letter to indicate the largest field width.
For example, % 3d indicates that three integer values are output, and the three digits are not right aligned.
% 9.2f indicates the floating point number with the output field width of 9, where the decimal point is 2 and the integer is 6,
The decimal point occupies one place, which is not 9 digits right-aligned.
% 8 s indicates the output string of 8 characters, which is not 8 characters in the right alignment.
If the length of a string, or the number of integer digits exceeds the field width, It is output according to the actual length.
However, for floating-point numbers, if the number of digits in the integer part exceeds the width of the entire digit, the data is output according to the actual integer;
If the decimal point width exceeds the specified decimal point width, the output is rounded to the specified width.
In addition, if you want to add some 0 before the output value, you should add 0 before the field width.
For example, % 04d indicates that when a value smaller than four digits is output, 0 is added before to make the total width
The value is 4 digits.
If a floating point is used to represent the output format of a character or integer, the number after the decimal point represents the maximum width,
The number before the decimal point represents the minimum width.
For example, % 6.9 s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If the value is greater than 9
Content after 9th characters will be deleted.
(2) You can add the lower-case letter l between "%" and the letter to indicate that the output is a long number.
For example, % LD indicates the output long integer.
% Lf indicates that the double floating point number is output.
(3) You can control the Left or Right alignment of the output, that is, you can add a "-" number between "%" and letters.
The output is left-aligned. Otherwise, the output is right-aligned.
For example, %-7d indicates that the output 7-digit integer is left aligned.
%-10 s indicates that the output is left aligned with 10 Characters
2. Some special characters
When there are too many threads, there are too many threads, too many threads.
Character Function
--------------------------
/N line feed
/F clear the screen and change the page
/R press ENTER
/T Tab character
/Xhh indicates an ascii code in hexadecimal notation,
HH indicates one to two hexadecimal numbers.
When there are too many threads, there are too many threads, too many threads.
Example 1
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char C, S [20], * P;
Int A = 1234, * I;
Float F = 3.141592653589;
Double X = 0.12345678987654321;
P = "How do you do ";
Strcpy (S, "Hello, comrade ");
* I = 12;
C = '/x41 ';
Printf ("A = % d/N", a);/* the output result is a decimal integer a = 1234 */
Printf ("A = % 6D/N", a);/* output 6-digit decimal number A = 1234 */
Printf ("A = % 06d/N", a);/* output 6-digit decimal number A = 001234 */
Printf ("A = % 2D/N", a);/* A exceeds 2 bits and Outputs A = 1234 */
Printf ("* I = % 4D/N", * I);/* output a four-digit decimal integer * I = 12 */
Printf ("* I = %-4D/N", * I);/* the output is left aligned with a four-digit decimal integer * I = 12 */
Printf ("I = % P/N", I);/* output address I = 06e4 */
Printf ("F = % F/N", f);/* output floating point number f = 3.141593 */
Printf ("F = 6.4f/N", f);/* output 6 floating point numbers with four decimal places
F = 3.1416 */
Printf ("x = % lf/N", x);/* output long floating point number x = 0.123457 */
Printf ("x = % 18.16lf/N", x);/* output a long floating point with 16 digits after the decimal point.
Number X = 0.1234567898765432 */
Printf ("c = % C/N", c);/* output character c = */
Printf ("c = % x/N", c);/* the ASCII code value of the output character c = 41 */
Printf ("s [] = % s/n", S);/* output array string s [] = Hello, Comrade */
Printf ("s [] = % 6.9 S/N", S);/* output string s [] = Hello,
Co */
Printf ("s = % P/N", S);/* output array string first character address S = ffbe */
Printf ("* P = % s/n", P);/* output pointer string P = How Do You do */
Printf ("P = % P/N", P);/* output pointer value p = 0194 */
Getch ();
Retunr 0;
}
The address values in the preceding results may be different on different computers.
Ii. scanf () function
The scanf () function is a formatting input function that reads input information from the standard input device (keyboard.
The call format is:
Scanf ("<formatted string>", <Address Table> );
The formatted string contains the following three types of characters;
1. format specifiers: The format specifiers are basically the same as those in the printf () function.
2. Blank characters: the blank characters will slightly remove one or more
White space characters.
3. Non-blank characters: A non-blank character will remove the scanf () function from reading
Characters with the same white space characters.
The address table is the address of all variables to be read, not the variable itself. This is the same as the printf () function.
It is completely different. Pay special attention to it. The addresses of each variable are separated by commas.
Example 2:
Main ()
{
Int I, J;
Printf ("I, j =? /N ");
Scanf ("% d, % d", & I, & J );
}

In the preceding example, the scanf () function first reads an integer and then removes the comma (,).
And then read another integer. If "," is not found, the scanf () function terminates. If
If the delimiter between parameters is space, one or more spaces must be entered between parameters.
Note:
(1). For string array or string pointer variables, the array name and pointer variable name are
Is the address, so when using the scanf () function, you do not need to add the "&" operator before them.
Example 3
Mian ()
{
Char * P, STR [20];
Scanf ("% s", P);/* input string from the drive */
Scanf ("% s", STR );
Printf ("% s/n", P);/* output string to the screen */
Printf ("% s/n", STR );
}

(2) You can add an integer between the formatting operators "%" in the formatted string to indicate
The maximum number of digits in any read operation.
In Example 3, if only 10 characters can be entered to the string pointer P, the first scanf () function statement
Change
Scanf ("% 10 s", P );
When the program is running, once the number of input characters is greater than 10, P will not continue to read, and the next read
The input function is scanf ("% s", STR), which is read from 11th characters.
There is a problem when using the scanf () function. The following is an example:
When multiple scanf () functions are used to continuously Input Multiple character variables, for example:
Main ()
{
Char C1, C2;
Scanf ("% C", & C1 );
Scanf ("% C", & C2 );
Printf ("C1 is % C, C2 is % C", C2/1, C2 );
}

Run the program, enter a character, and then press enter (to complete the input, you must press Enter ).
("% C", & C1), assign "a" to the variable C1, but the carriage return remains in the buffer, and run the input statement.
Scanf ("% C", & C2), the variable C2 outputs an empty row. If you enter AB and press enter, the output ends.
Result: C1 is A, C2 is B.
To solve the above problem, you can add the clear function fflush () before the input function (this function enables
The method will be described at the end of this section ). Modify the preceding program:
# Include <stdio. h>
Main ()
{
Char C1, C2;
Scanf ("% C", & C1 );
Fflush (stdin );
Scanf ("% C", & C2 );
Printf ("C1 is % C, C2 is % C", C1, C2 );
}

1.1.2 non-formatted input/output functions
The non-formatted input/output function can be replaced by the standard formatted input/output function described above,
After these functions are compiled, the code is small, and the memory usage is relatively small, which increases the speed.
Convenience. The following is an introduction.
I. Puts () and gets () Functions
1. Puts () function
The puts () function is used to write a string to the standard output device (screen) and wrap the line. The call format is:
Puts (s );
S is a string variable (string array name or string pointer ).
The puts () function works the same way as printf ("% s/n", S.
Example 4:
Main ()
{
Char s [20], * F;/* defines the string array and pointer variable */
Strcpy (S, "Hello! ");/* Assign values to string array variables */
F = "thank you";/* assign values to string pointer variables */
Puts (s );
Puts (f );
}

Note:
(1). The puts () function can only output strings and cannot output numeric values or perform format conversion.
(2). Strings can be directly written to the puts () function. For example:
Puts ("Hello, Turbo c2.0 ");

2. Gets () function
The gets () function is used to read a string from the standard input device (keyboard) until the carriage return ends, but the carriage return
Does not belong to this string. The call format is:
Gets (s );
S is a string variable (string array name or string pointer ).
The gets (s) function is similar to scanf ("% s", & S), but not exactly the same. Use scanf ("% s", & S)
A problem occurs when the function inputs a string, that is, if a space is entered, the input string ends,
The character after the space is processed as the next input, but the gets () function will receive the entire input character
String until press Enter.
Example 5
Main ()
{
Char s [20], * F;
Printf ("What's your name? /N ");
Gets (s);/* Wait for the input string until the carriage return ends */
Puts (s);/* output the input string */
Puts ("How old are you? ");
Gets (f );
Puts (f );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.