Hands-on brain class exercises, hands-on brain class

Source: Internet
Author: User

Hands-on brain class exercises, hands-on brain class

I,Source code, reverse code, and supplemental code

(Source: http://www.cnblogs.com/zhangziqiu)

1,Machine count and real value

Before learning the original code, reverse code, and complement code, you must first understand the concept of the number of machines and the real value.

(1) number of machines

The binary representation of a number in a computer, called the number of machines. The number of machines is signed. in a computer, the highest bit of a number is used to store the symbols. The positive number is 0, and the negative number is 1. for example, if the number is + 3 in decimal format and the computer word length is 8 bits, the conversion to binary format is 00000011. -3 means 10000011. In this case, 00000011 and 10000011 are the number of machines.

(2) True Value

Because the first digit is the symbol bit, the form value of the machine number is not equal to the real value. For example, if the above signed number is 10000011, its highest bit 1 indicates negative, and its real value is-3 instead of the form value 131 (10000011 is converted to decimal equal to 131 ). Therefore, for the sake of distinction, the real value corresponding to the number of machines with signs is called the true value of the number of machines. Example: the true value of 0000 0001 = + 000 0001 = + 0001 0001 =-000 =-1

2,Original code

The original code is the absolute value of the symbol bit plus the true value, that is, the first digit represents the symbol, and the other digits represent the value. For example, if it is an 8-bit binary:

[+ 1] original = 0000 0001

[-1] original = 1000 0001

The first digit is the symbol bit. Because the first digit is the symbol bit, the value range of the 8-bit binary number is:

[1111 1111,011 1 1111]

That is, [-127,127]

The original code is the easiest way for the human brain to understand and calculate.

3,Reverse code

The expression of the anti-code is as follows:

The inverse code of positive numbers is itself

The negative number is based on the original code, the symbol bit remains unchanged, and the other bits are reversed.

[+ 1] = [00000001] original = [00000001]-

[-1] = [10000001] original = [11111110] Reverse

It can be seen that if an anticode represents a negative number, the human brain cannot intuitively see its value. It is usually converted to the original code and then computed.

4,Complement

The expression of the complement code is as follows:

Positive complement is itself

The complement of a negative number is based on the original code, the symbol bit remains unchanged, and the rest of you get the reverse code. Finally, + 1 (that is, + 1 on the basis of the reverse code)

[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement

[-1] = [10000001] original = [11111110] = [11111111]

For negative numbers, the human brain cannot directly see the value of the complement expression. It is usually necessary to convert the value to the original code to calculate its value.

5,Java uses the complement code to perform various bitwise operations on positive and negative numbers.

Now we know that the computer can have three encoding methods to represent a number. For positive numbers, the results of the three encoding methods are the same:

[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement

So there is no need to explain too much, but for negative numbers:

[-1] = [10000001] original = [11111110] = [11111111]

It can be seen that the original code, the reverse code and the complement code are completely different. Since the original code is directly recognized by the human brain and used for computation and representation, why is there a reverse code and a complement?

First, because the human brain can know that the first digit is the symbol bit, During computation, We will select the addition and subtraction of the true value area based on the symbol bit. (the concept of true value starts at the beginning of this article ). however, for computers, the addition and subtraction of multiplier is already the most basic operation, and the design should be as simple as possible. computer identification of "symbol bit" will obviously make the computer's basic circuit design very complex! So people came up with a method to include the symbol bit in the operation. we know that, according to the algorithm minus a positive number equals to adding a negative number, that is, 1-1 = 1 + (-1) = 0, so the machine can only add and not subtract, in this way, the design of computer operations is simpler. as a result, people began to explore the method of adding the symbol bit to the operation. first, let's look at the original code:

Decimal expression: 1-1 = 0

1-1 = 1 + (-1) = [00000001] original + [10000001] original = [10000010] original =-2

If the original code is used for calculation, the result is incorrect for subtraction. That is why the computer does not use the original code to represent a number.

In order to solve the problem of original code subtraction, there is a reverse code:

Decimal expression: 1-1 = 0

1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] + [1111 1110] = [1111 1111] = [1000 0000] =-0

It is found that the negative code is used to calculate and subtract, and the true value of the result is correct. the only problem actually occurs in the special value "0. although we understand that + 0 is the same as-0, 0 is meaningless. in addition, there will be two encoding formats: [0000 0000] original and [1000 0000] original, indicating 0.

The occurrence of the complement solves the problem of the zero symbol and two encodings:

1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] supplement + [1111 1111] Supplement = [0000 0000 0000] Supplement = [0000] original

In this way, 0 is represented by [0000 0000], and-0, which previously encountered a problem, does not exist. In addition, [1000 0000] can be used to represent-128:

(-1) + (-127) = [1000 0001 1111] original + [1111 1111 1111] original = [1000 0001] supplement + [1000 0000] Supplement = []

-The result of 1-127 should be-128. in the result of the complement operation, [1000 0000] is-128. however, because the previous-0 complement code is used to represent-128,-128 does not represent the original code or reverse code. ("-128" indicates that the source code [1000 0000] is [0000 0000], which is incorrect)

Using the complement Code not only fixes the zero sign and two encodings, but also represents a minimum number. this is why the range of the 8-bit binary code expressed by the original code or the back code is [-127, + 127], and the range indicated by the complement code is [-128,127].

Because the machine uses the complement code, the 32-bit int type commonly used in programming can be expressed in the range of [-231,231-1] Because the first digit represents the symbol bit. you can save one more minimum value when using the complement representation.

Ii. Read the example carefully: EnumTest. java, run it, and analyze the running result?

What conclusions do you get?

1. source program:

PackageDemo;

Public ClassEnumTest {

Public Static VoidMain (String [] args ){

Size s = Size.SMALL;

Size t = Size.LARGE;

// S and t reference the same object?

System.Out. Println (s = t );

// Is the original data type?

System.Out. Println (s. getClass (). isPrimitive ());

// Convert from string

Size u = Size.ValueOf("SMALL ");

System.Out. Println (s = u); // list all its values

For(Size value: Size.Values()){

System.Out. Println (value );

}

}

}

 EnumSize {SMALL,MEDIUM,LARGE};

2. Results:

 

3,Result Analysis: Expression of Enumeration type: enum Size {SMALL, MEDIUM, LARGE}

Usage: Size s = Size. SMALL;

// Convert from string to Enumeration

Size t = Size. valueof ("SMALL ");

The enumeration type is a reference type. enumeration does not belong to the original data type. Each specific value of enumeration references a specific object. The same value references the same object. You can use the "=" and equals () Methods to directly compare the values of enumerated variables. In other words, for enumerated variables, "=" and equals () the result of method execution is equivalent. When determining whether two values are the same, the output result can only be true or false. When the value is output, the number of times the Size value: Size. values () loop type is required. Enum Size {SMALL, MEDIUM, LARGE}; the statement can be written at the end of the program or at the beginning, but must be referenced by yourself. The enumerated values can be expressed in two ways: 1. Size s = Size. SMALL; 2. Size t = Size. valueof ("SMALL ").

3. Java variables follow the "blocking principle of variables with the same name". Read the relevant information and write some test code by yourself, just like in this example, consciously define variables with the same name in different places to see which value is output.

1. source program:

PackageDemo;

Public ClassMy1 {

Private Static Int Value= 1;

Public Static VoidMain (String [] args ){

IntValue = 2;

System.Out. Println (value );

}

}

Result: 2

2. source program:

PackageDemo;

Public ClassMy1 {

Private Static Int Value= 10;

Public Static VoidMain (String [] args ){

IntValue = 6;

System.Out. Println (value );

}

}

Result: 6

3. Conclusion: The assignment in a function takes precedence over the assignment outside the function and is a local variable. Values outside a function can be assigned to multiple functions in the class, which are global variables. If there are no repeated values in the function, the value of the function is outside the function.

4. Look at the figure and check the number of digits of each data type in Java and the range of values. What conclusions can you draw??

A: automatic type conversion is secure. Forced type conversion may cause information loss. The solid line indicates no loss of precision, and the dotted line indicates a loss of precision. Generally, both ends of the solid line point from low precision to high precision, and the Occupied number of digits ranges from small to large, therefore, it can be concluded that the conversion from low-precision to high-precision will not lose the precision, but from high-precision to low-precision will be lost.

5. Run the following code (TestDouble. java). What kind of output do you see?

A: The result is

 

Unexpectedly, when a value of the double type is used for calculation, the result is inaccurate. A computer can only recognize binary data. All data must be converted to binary data. For example, in the source program, 0.05 is in decimal format and must be converted to binary format. However, the binary type of 0.05 is not accurate to 0.05, but close to 0.05. The actual binary type is 0.04999, 999, 999, 999, and 999. Floating Point Numbers are composed of two parts: when the index and tail number are converted to the binary and decimal values of the floating point number, the floating point number is involved in the calculation, and the conversion process becomes unpredictable and irreversible.

6. When constructing a BigDecimal object, use a string instead of a double value. Otherwise, the calculation accuracy may still be affected. (Why ?)

PackageDemo;

ImportJava. math. BigDecimal;

Public ClassTestBigDecimal

{

Public Static VoidMain (String [] args)

{

BigDecimal f1 =NewBigDecimal ("0.05 ");

BigDecimal f2 = BigDecimal.ValueOf(0.01 );

BigDecimal f3 =NewBigDecimal (0.05 );

System.Out. Println ("calculation result of using String as the BigDecimal constructor parameter below :");

System.Out. Println ("0.05 + 0.01 =" + f1.add (f2 ));

System.Out. Println ("0.05-0.01 =" + f1.subtract (f2 ));

System.Out. Println ("0.05*0.01 =" + f1.multiply (f2 ));

System.Out. Println ("0.05/0.01 =" + f1.divide (f2 ));

System.Out. Println ("calculation result of using double as the BigDecimal constructor parameter below :");

System.Out. Println ("0.05 + 0.01 =" + f3.add (f2 ));

System.Out. Println ("0.05-0.01 =" + f3.subtract (f2 ));

System.Out. Println ("0.05*0.01 =" + f3.multiply (f2 ));

System.Out. Println ("0.05/0.01 =" + f3.divide (f2 ));

}

}

A: The result is

It can be seen that constructing a BigDecimal object can solve the problem of inaccuracy. However, you should use a string instead of a double value. Otherwise, the computing accuracy may still be affected.

7. What is the output result of the following code?

Int X = 100;

Int y= 200;

System. out. println ("X + Y =" + X + Y );

System. out. println (X + Y + "= X + Y ");

Why is this output?

Answer: source program:

PackageDemo;

Public ClassYouxi {

Public Static VoidMain (String [] args ){

IntX = 100;

IntY = 200;

System.Out. Println ("X + Y =" + X + Y );

System.Out. Println (X + Y + "= X + Y ");

}

}

Result:

In System. out. in println (), if the string is followed by the plus sign and the variable, the variable is converted to the string type. the plus sign is used to connect the two strings into a new string for output; if a variable is added or subtracted before a string is added, the addition and subtraction of the variable is calculated from left to right, and then combined with the subsequent string to form a new string. That is to say, the plus sign is connected only when two string types or one of them is string type. Otherwise, it is still an operator.

8,0 in Java

A: The result of 0.0/0.0 is NaN (not an number in short, that is, "not a number "). It is determined by Double. isNaN (double x.

Positive number/0.0 returns positive infinity, that is, Infenity

Negative/0.0 returns negative infinity, that is, Infenity. It is determined by Double. isInfinite (double x.

Source program:

PackageDemo;

Public ClassMy3 {

Public Static VoidMain (String [] args ){

DoubleA = 0,

B = 50,

C =-50;

System.Out. Println ("0/50 = \ n" + (a/B ));

System.Out. Println ("0/(-50) = \ n" + (a/c ));

System.Out. Println ("-50/0 = \ n" + (c/));

System.Out. Println ("0/0 = \ n" + (a/));

System.Out. Println ("50/0 = \ n" + (B/));

}

}

Result:

 

 

 

Related Article

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.