Home  >  Article  >  Java  >  I secretly looked at some computer basics, and from now on learning Java is like cheating!

I secretly looked at some computer basics, and from now on learning Java is like cheating!

Java学习指南
Java学习指南forward
2023-07-26 17:30:051708browse

Before learning Java, I would like to ask a question. Do you think it is necessary to learn computer basics?

Many people think that there is no need to read those boring and obscure basic knowledge. It is better to start directly from HelloWorld. First touch the program, get an impression, and the code will run, and then gradually learn the syntax of the program in depth, and finally use it. Program building projects, that is, practical learning methods.

First of all, I do not deny this way of learning. It is indeed more suitable for some students, especially those who are anxious to find a job. Learn how to use it first. As for the principle, you can slowly learn it later. Dig deeper, after all, nothing is more important than bread?

But for our students who are learning logarithms from scratch, I still recommend starting with some basic knowledge of computers to understand the ideas and common sense of programming, which will also be very helpful for our future study. It's like building a house. Some people build thatched houses, some build mud houses, and some people want to build a two-story Western-style house. The depth of the foundation directly determines the level of future achievements.

So now let’s briefly understand some basic knowledge of computers.

1. Classification of machine language

Machine language

Machine language is a language that a computer can directly recognize. It is a computer language expressed directly in binary code instructions. It is a code composed of a string of 0s and 1s, with a certain number of digits, and is divided into several segments. The coding of each segment represents a different meaning. For example, the following is a simple string of machine codes:

010100100000  // 520

So many people ask, why is the machine code composed of 0 and 1?

Because the machine code needs to control the computer hardware to respond to the program instructions, 0 represents low potential, 1 represents high potential, so that a logic circuit can be generated, which is equivalent to controlling a switch. 0 is closed and 1 is open. .

Assembly language

Assembly language is a language for developers. Since the machine language is all 0 and 1, it is difficult for developers to directly control and use it, so Some special symbols need to be used as markers for binary code. Developers input these special symbols to complete the issuance of instructions and let the computer work for us. These special symbols are assembly language. Computers cannot directly recognize assembly language, and a software is required to translate assembly language into machine language. The difference between it and machine language lies in the representation method of instructions. The main body of assembly language is assembly instructions. Compared with machine instructions, programmers are easier to remember.

MOV AX,1234H  //汇编指令: 寄存器AX的内容送到1234H中
101110000011010000010010 //机器指令

High-level language

High-level languages ​​are common such as: c, c, java, python, php, etc.

It is closer to our normal thinking. Its biggest feature is that it is easy to write and the code is readable. To achieve the same function, it takes less time to use high-level languages, the program code is shorter, and it is easier to read. Second, high-level languages ​​are portable, that is, a piece of code can be run on different types of computers with little or no modification.

print('Hello World')   // python版HelloWorld

我们从这个程序可以看出来,高级语言屏蔽了机器内部指令运行细节,我们可以像写作一样书写程序,而不用关心语言内部的实现细节,这大大提高了我们的开发效率,节约开发成本

当然,其缺点也很明显,使用高级语言编写的程序运行时,需要先将其翻译成低级语言计算机才能运行它,在翻译过程中可能会产生一些多余的部分,运行效率低些。另外,对硬件的可控性相对于低级语言弱些,目标代码量较大


二. 进制

推荐使用在线工具进行进制转换

https://tool.oschina.net/hexconvert/

二进制

由数字0和1组成,逢二进一,比如机器码就是二进制的,是最简单的计算机可读懂的代码,例如 0101(表示十进制数字5)。

八进制

由1到7组成的数字串,数字最大不会超过7,逢八进一,例如 157(表示十进制数字111)

十进制

我们日常使用的数字都是十进制类型的,逢十进一,例如  0123456789。

十六进制

由1到9,a-f(或者是A-F,分别代表10-15)组成的数字串,数字最大不会超过15,其中字母是不区分大小写的,逢十六进一,例如0F83(表示十进制数3971)

进制转换

1. K进制与十进制数的转换

假设有一个n+1位的K进制数,它的形式如下:

AnAn-1…A3A2A1A0
则它的大小为:(也就是对应的我们能看懂的十进制数为)

A0 * K^0 + A1 * K^1....+ An * K^n      //K^n表示K的n次方

二进制数:10101 转换成 十进制数为:21

 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1+1*2^0 = 21

2. 十进制与k进制的转换

短除法。

举个栗子:

I secretly looked at some computer basics, and from now on learning Java is like cheating!


从图可以看出,用十进制数21一直除以2,每次得到的余数倒数就是最后的二进制数10101。同样,十进制转八进制、十进制转十六进制都是一样的套路,非常简单。



3. 二进制与八进制和十六进制之间转换


8是2的3次方,16是2的4次方,所以这之间的转换存在一种快捷方法。以2转8示例,将2进制从低位到高位,每3个一组,如果是十六进制就每4个一组,高位不足3位的补0,然后将每组依次转换成对应的十进制,得到的结果就是对应的8进制或者16进制。

二进制10101100101转八进制:2545

I secretly looked at some computer basics, and from now on learning Java is like cheating!

二进制10101100101转十六进制:565
I secretly looked at some computer basics, and from now on learning Java is like cheating!

三. 原码、反码、补码

在计算机中,最小的单位是位,也称为比特(bit)。而另一个常用单位是字节,一个字节是8位,也就是8比特,所以我们常用的二进制表示法是8位。

原码

原码是一种非常常见的二进制表示形式。在原码中,为了区别正数和负数,将二进制中的最高位作为符号位,如果是0表示正数,如果是1表示负数。

举个栗子:

0000 0001   // 表示 1
1000 0001   // 表示 -1


反码

不知道大家有没有注意到原码的一个问题,那就是负数参与计算的时候,比如

I secretly looked at some computer basics, and from now on learning Java is like cheating!

出现了一个大问题,就是1 + (-1) 不等于0,而等于 -2。


这可咋整?

In order to solve this problem, smart computer predecessors thought of reverse coding. The rules for converting original code to inverse code are: The inverse code of a positive number is itself, the inverse code of a negative number is that the sign bit remains unchanged, and other bits are inverted. The rule of negation is that if it is 0, it becomes 1, and if it is 1, it becomes 0.
Let’s take a look at the calculation of converting to the inverse code:

I secretly looked at some computer basics, and from now on learning Java is like cheating!

The result obtained is 1111 1111.

Hey? This is wrong, why is it not 0?

Don’t worry, this is just the calculation result of the inverse code. We convert the inverse code into the original code 1111 1111 —> 1000 0000, and get -0, which is 0, which is completely in line with the expected result and solved. Calculation problem of original code.


Complement code

The complement code solves the problem of negative number calculation, but there is still one problem that has not been solved, which is -0. Due to the existence of the sign bit of the highest bit of a negative number, the original eight-bit binary number can represent 2 to the 8th power, that is, 256 numbers. However, using the original code and the complement code can only represent 255, which is very uncomfortable for us. , so how can we make up for this missing number?

The bald programmers also came up with the corresponding solution - code complement.

Rules for converting original code to complement code: The complement code of a positive number is itself, the complement code of a negative number is that the sign bit remains unchanged, and the remaining digits are inverted (that is, they become the complement code) and then added 1.

For example:

Original code: 0000 0001, complement code: 0000 0001
Original code: 1000 0001, complement code: 1111 1111

Calculate:

I secretly looked at some computer basics, and from now on learning Java is like cheating!

#As can be seen from the above, using complement calculation we get 0 (instead of -0), which solves the problem of one less number.


In the complement code, it is specified that 0 is represented as 0000 0000, and 1000 0000 is represented as -128. Note that this is a rule.


Notes

  1. One's complement and one's complement cannot be directly used to convert binary to decimal. To get the corresponding size into the corresponding decimal system, you should first convert it into the original code. In other words, the original code is a form of expression that is directly related to the size

  2. In computer systems, values ​​​​are always represented and stored in the form of complement numbers

  3. The original code, complement and complement of positive numbers are the same

  4. The original code of negative numbers is inverted: the sign bit remains unchanged, and the remaining bits are inverted.

  5. Negative numbers are converted to complement: the sign bit remains unchanged, and the remaining bits are inverted and then plus one

  6. Negative numbers are complemented to complement: the sign bit remains unchanged , the remaining bits are reduced by one

  7. The negative number is converted to the original: the sign bit remains unchanged, the remaining bits are reduced by one and then inverted

The above is the detailed content of I secretly looked at some computer basics, and from now on learning Java is like cheating!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Java学习指南. If there is any infringement, please contact admin@php.cn delete