Java Basics (2)

Source: Internet
Author: User

In-process

Basic, purpose: to understand that a computer can handle only 2 of the data and instructions in the system

1) 10 binary counting rule

Numbers: 0 1 2 3 4 5 6 7 8 9

Base: 10

Right: 1000 100 10 1 The right is the N power of the base

2) 2 binary counting rule

Numbers: 0 1

Base: 2

Right: 128 64 32 16 8 4 2 1 The power is the N power of the cardinality

(10) (2)

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

Expanded type:

11001010 (2) = 128+64+8+2 = 202 (10)

234 (10) =? (2)

128 64 32 16 8 4 2 1

234 (10) = 1 1 1 0 1 0 1 0 (2)

106 42 10) 2 0

Binary complement! Cost considerations, to save money!

Take 4-bit complement as an example

1) A large number with a high position of 1, use as negative numbers! The highest bit is called the sign bit.

2) If the calculation time exceeds 4 digits, the multi-digit automatic overflow discards.

3) In the case of not exceeding the range, the complement arithmetic satisfies the mathematical rule

4) The disadvantage number has the scope, cannot carry on the super-range computation

The complement of the anti-call phenomenon:-n = ~n + 1

symbol bit expansion phenomenon : the low-number complement expands to the high-complement : Positive complement 0 Negative complement 1

(10) 4-bit complement 8-bit complement 32-bit complement

Min--------10000000 00000000 00000000 00000000

8 0 0 0 0 0 0 0

...

-129--------11111111 11111111 11111111 01111111

-128----10000000 11111111 11111111 11111111 10000000

-127----10000001

-126----10000010

....

-10----11110110

-9----11110111

-8 1000 11111000

-7 1001 11111001

-6 1010 11111010

-5 1011 11111011

-4 1100 11111100

-3 1101 11111101 11111111 11111111 11111111 11111101

-2 1110 11111110 11111111 11111111 11111111 11111110

-1 1111 11111111 11111111 11111111 11111111 11111111

0 0000 00000000 00000000 00000000 00000000 00000000

1 0001 00000001 00000000 00000000 00000000 00000001

2 0010 00000010 00000000 00000000 00000000 00000010

3 0011 00000011 00000000 00000000 00000000 00000011

4 0100 00000100

5 0101 00000101

6 0110 00000110

7 0111 00000111

8----00001000

9----00001001

Ten----00001010

...

126----01111110

127----01111111 00000000 00000000 00000000 01111111

------------00000000 00000000 00000000 10000000

129------------00000000 00000000 00000000 10000001

....

Max------------01111111 11111111 11111111 11111111

7 F F F f f f f

The code implements the output-128 to 127 binary:

for (int i=-128;i<=127;i++) {

String bin=integer.tobinarystring (i);

int N=32-bin.length ();

for (int j=0;j<n;j++) {

System.out.print ("0");

}

System.out.println (BIN);

}

-3 1101

-3 1101

+ 11 1

-------------

-6 1010

-5 Complement:-5 = + 1

0101

1010

1011

16 Binary counting Rule

Numbers: 0 1 2 3 4 5 6 7 8 9 a B c D E F

Base: 16

Weight: 256 16 1

Power is the n power of cardinality

16 binary is used to simplify the writing of 2 binary!

2 binary data per 4 bits can be abbreviated to 1-bit 16 decimal

16 binary is 2 binary!

10 16 2

0 00 0000 0000

1 01 0000 0001

2 02 0000 0010

3 03 0000 0011

4 04 0000 0100

5 05 0000 0101

6 06 0000 0110

7 07 0000 0111

8 08 0000 1000

9 09 0000 1001

0a 0000 1010

0b 0000 1011

0c 0000 1100

0d 0000 1101

0e 0000 1110

0f 0000 1111

16 10 0001 0000

17 11 0001 0001

18 12 0001 0010

...

65 41 0100 0001

66 42 0100 0010

...

192 C0 1100 0000

193 C1 1100 0001

...

255 FF 1111 1111

System.out.println (010+2);//10

Summary of the binary:

1) The computer can only process 2 data (often the complement)!

2) There is No 10 binary and 16 binary inside the computer

3) 10 binary is the habit of human processing data, Java uses the API to provide algorithms (methods) to achieve 10 binary processing!

4) 16 binary is a convenient 2-input writing format!

5) All the processing data to the computer to be converted into 2!

Original Code Anti-code complement

1) The original code refers to the highest bit as the sign bit (0 means positive, 1 is negative), the other digits represent the absolute value of the numeric representation of the number.
For example: The number 6 in the computer is represented by the code: 00000110

The result of the multiplication operation is correct when using the original code with the sign bit, but there is a problem in the addition and subtraction operation, as follows:
(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10
(00000001) original + (10000001) original = (10000010) original = (-2) is obviously incorrect.

2) The anti-code representation rule is: if it is positive, it means the same method as the original code, and if it is negative, the symbol bit 1 is preserved.

Then the original code of this number is reversed for each bit, then the inverse code representation of the number is obtained.

For example, the inverse code of the number 6 in a 8-bit computer is its original code: 0000 0110

The number (-6) in the 8-bit computer's anti-code is: 11111001

Because there is no problem in the addition operation of two integers, it is found that the problem appears on a negative number with a sign bit,

The inverse code is generated for the rest of you, except for the sign bit. The inverse code value space is the same as the original code and corresponds to one by one. The following is the subtraction operation of the inverse code:
(1) 10-(1) 10 = (1) + + (-1) 10 = (0) 10
(00000001) counter + (11111110) counter = (11111111) counter = (-0) there is a problem.
(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10
(00000001) Anti-+ (11111101) counter = (11111110) counter = (-1) correct

The problem arises on (+0) and (-0), and 0 is not positive or negative in people's computational concepts. Then the concept of complement was introduced. The complement of negative numbers is to add one to the inverse code, and the positive number of the original code is the same. In the complement (-128) instead of (-0), so the complement of the expression range is: ( -128~0~127) a total of 256.

3) So the complement is designed to:

⑴ allows the symbolic potential energy to participate in the operation with the valid Values section, simplifying the operation rules.
⑵ the subtraction operation into addition, further simplifying the circuit design of the computing device in the computer

Java Basics (2)

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.