[GO Language Fundamentals] GO Carry Counting System (Base), Identifiers and Keywords (5)

Base

The base is also the carry counting system, which is an artificially defined counting method with carry (there are counting methods without carry, such as the original knot counting method, the "positive" word counting method commonly used when singing votes, and the like tally mark count). For any kind of base-X base, it means that the number at each position is incremented every X. Decimal is one in every decimal, hexadecimal is one in every hexadecimal, and binary is one in every two, and so on, the x system is every x.

All modern electronic computer technology uses binary system, because it only uses two digital symbols, 0 and 1, which is very simple and convenient, and easy to implement electronically. The information processed inside the computer is represented by binary numbers. Binary numbers use the two numbers 0 and 1 and their combination to represent any number. The carry rule is "every 2 enters 1". The number 1 represents different values ​​in the same bit. In the order from right to left, this value is increased by two times.

Inside the computer, when running various operations, they all run in a binary manner.

For integers, the commonly used bases are binary, octal, decimal, and hexadecimal

  • Binary: 0, 1, full 2 ​​into 1
  • Octal: 0-7, full 8 into 1, and the computer starts with 0
  • Decimal: 0-9, full 10 into 1
  • Hexadecimal: 0-9 and AF, full hexadecimal into 1, the computer starts with 0x or 0X. The AF here is not case sensitive, such as 0x21AF+1 = 0X21B0

Comparison of different bases

Decimal Hexadecimal Octal Binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000

Base conversion

Convert other bases to decimal

Rule : Starting from the lowest digit (on the right), extract the number in each digit, multiply it by the power of 2 (digit -1), and then sum.

  • Binary to decimal
    1011 = 1 * 23+0 * 2 2+1 * 2 1+1 * 20 = 8 + 2 + 1 = 11
  • Octal to Decimal
    01011 = 1 * 83 + 0 * 82 + 1 * 81 + 1 * 80 = 521
  • Hexadecimal to Decimal
    0x1011 = 1 * 163 + 0 * 162 + 1 * 161 + 1 * 160 = 4113

Decimal to other bases

  • Decimal to binary
    rule : Keep dividing the number by 2 until the quotient is 0, and then invert the remainder obtained at each step, which is the corresponding binary.

  • Decimal to octal
    rule : Keep dividing the number by 8 until the quotient is 0, and then invert the remainder of each step, which is the corresponding octal system.

  • Decimal to hexadecimal
    rule : Keep dividing the number by 16 until the quotient is 0, and then invert the remainder of each step, which is the corresponding hexadecimal.

Binary to other bases

  • The
    rule of binary conversion to octal : a group of every three digits of the binary number (combined from the low digits), and convert it into the corresponding octal number.
    11010101 => (11)(010)(101) => 0325
  • The
    rule of binary conversion to hexadecimal system : convert every three digits of binary numbers (combined from the low digits) into corresponding octal numbers.
    11010101 => (1101)(0101) =>0xD5

Convert other hexadecimals to binary

  • Octal-to-binary conversion
    rule : Convert each digit of octal to a corresponding three-digit binary number.
    0237 => (010)(011)(111) => 10011111
  • The
    rule of hexadecimal conversion to binary system : convert each digit of octal system into a corresponding four-digit binary number.
    0x237 => (0010)(0011)(0111) => 1000110111

Original code, inverse code, complement

In a computer, fixed-point numbers are divided into signed numbers and unsigned numbers. Among them, there are three ways to represent the signed number: original code, inverse code and complement.

In the computer operation, the operation is performed in the way of complement.

The three representation methods have two parts: the sign bit and the value bit . The sign bit uses 0 to indicate "positive" and 1 to indicate "negative", while the value bit has different representation methods.

  • Original code The
    original code (true form) is a binary fixed-point representation of numbers in a computer. The original code representation adds a sign bit in front of the value (that is, the highest bit is the sign bit): positive numbers are 0, negative numbers are 1 (0 has two representations: +0 and -0), and the remaining bits Indicates the magnitude of the value.

Advantages:
simple and intuitive; for example, we use 8-bit binary to represent a number, the original code of +11 is 00001011, and the original code of -11 is 10001011

Disadvantages: The
original code cannot directly participate in the calculation, and errors may occur. For example, in mathematics, 1+(-1) = 0, and 00000001+10000001=10000010 in binary, converted to decimal is -2. Obviously something went wrong.

  • Inverse code The
    inverse code notation stipulates that the inverse code of a positive number is the same as the original code; the inverse code of a negative number is the inverse of the original code bit by bit, except for the sign bit.

  • Complement code The
    complement code notation stipulates that the complement of a positive number is the same as the original code; the complement of a negative number is added to the last bit of its complement.
    In computer systems, values ​​are always represented and stored in complements. The reason is that by using the complement code, the sign bit and the value field can be processed uniformly; at the same time, addition and subtraction can also be processed uniformly. In addition, the complementary code and the original code are converted mutually, and the operation process is the same, and no additional hardware circuit is required.

  • Representation of numbers

In the original code, there are two representations of 0 (+0) 0000 0000 and (-0) 1000 0000. This results in the non-uniqueness of the encoding mapping, which must be distinguished on the computer. However, +0 and -0 have no practical significance.

  • Number operation

In order to solve the above-mentioned number representation problem, we can force the converted 10000000 to be determined as -128. But there is a new problem that is the calculation of numbers. Mathematically, 1+(-1)=0, and 00000001+10000001=10000010 in binary, converted to decimal is -2. Obviously something went wrong. Therefore, the sign bit of the original code cannot directly participate in the operation, and must be separated from other bits, which increases the overhead and complexity of the hardware.
At this time, it is necessary to introduce the complement code, and the complement code notation stipulates: the complement of a positive number is the same as the original code; the complement of a negative number is added to the last bit of its complement. One's complement is defined as: the one's complement of a positive number is the same as its original code; the one's complement of a negative number is the inverse of its original code bit by bit, except for the sign bit.

  • Why introduce complements?

Solve the first problem first. The introduction of the complement code is to solve the problem of number representation and number operation in the computer. Using the complement code, the sign bit and the value field can be processed uniformly, that is, the reference to the modulus operation on the sign bit in mathematics Automatic processing, the natural processing of the sign bit is realized by the automatic discarding of the modulus, and the expected requirements can be fulfilled without changing the physical architecture of the machine only by changing the encoding.

Identifiers and keywords

Identifier

The character sequence used by Golang when naming various variables, methods, functions, etc. becomes an identifier

The names of functions, variables, constants, types, statement labels, and packages in Go follow a simple rule: the name starts with a letter (characters in Unicode) or underscore, and can be followed by any number of characters , numbers, and underscores. , And is case sensitive .

If an entity has a reputation in a function, it is only valid locally in the function. If declared outside the function, it will be visible to all source files in the package. The case of the first letter of an entity determines whether its visibility spans packages. If the name starts with a capital letter, it is exported, which means it is visible and accessible outside the package, and can be referenced by other programs outside of its own package, such as Printf in the fmt package. The package name always consists of lowercase letters.

Package name : Keep the package name consistent with the directory. Try to use meaningful package names that are short and meaningful, and do not conflict with the standard library.
Variable names, function names, and constant names: try to use the camel case method. The
first letter can be capitalized by other packages. Access (similar to Public), lowercase first letter can only be used in this package (similar to Private)

Keyword

Go has 25 keywords, which can only be used where the grammar allows. They cannot be used as variable names:

break            default            func            interface
select           case               defer           go
map              struct             chan            else
goto             package            switch          const
fallthrough      if                 range           type
continue         for                import          return
var

Predefined identifier

In addition, there are 36 built-in pre-declared constants, types and functions: (There is also a saying called pre-defined identifiers )

  • constant:
true            false            iota            nil
  • Types of:
int        int8        int16        int32        int64
uint       uint8       uint16       uint32       uint64      uintptr
float32    float64     complex64    complex128
bool       byte        rune         string       error
  • function:
make            len            cap            new            append           copy       
close           delete         complex        real           imag             panic        
recover

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/113934057