This is the 13th day of my participation in the August Challenge. Check out the August Challenge for more details

Why learn the basics of programming

Because you’re the programmer first, the front end second.

A programmer needs to know

Hardware and Software: The operation principle of computer (Coding), as well as college computer related courses, such as Computer Composition Principle, Computer Network, when I was in college, the teacher talked about computer network

Biggest software: Operating system (Wikipedia) Know what constitutes an operating system

Write software: Data structure & Algorithm (” Data Structure and Algorithm Analysis “) data structure and algorithm analysis, sorting algorithm

Writing Software for many People: Software Engineering

Why should the front end learn programming basics?

  • The operating system runs on hardware
  • Browsers run on top of operating systems, not just browsers, but all software runs on top of operating systems.
  • HTML/CSS/JS runs on top of the browser
  • HTML/CSS/JS and data come from the server

There are three levels of dependency, so our front-end needs to learn the basics of programming before we know

  • How does CSS/JS work?
  • Where are JS variables stored?
  • How does the data get from the server?

Computers: The binary world

  1. Why can computers only store zeros and ones?
  2. How do I store zeros and ones
  3. How to store numbers
  4. How to store characters
  5. How to Store Chinese
  6. How do I store all characters
  7. How to use less space for storage

1. Why can computers only store zeros and ones?

This involves a lot of hardware knowledge, to put it simply, because the computer is an integrated circuit, the memory is microscopic countless small switches, or small batteries, and “0” and “1” just respectively represent “off” and “on” (or “not energized” and “energized”), just like the light switch in the home only on and off. So, by its very nature, a computer must be binary, storing only zeros and ones

2. How to store 0 and 1

A stored procedure in a computer is simply the process of constantly recharging a small battery, which consumes its own power (just as the Nanfu battery at home consumes its own power when left unused for a long time). So, since the small battery consumes its own power and needs to be recharged constantly, how can it be determined to be in a state of zero and one? The rule is: when the small battery power is greater than 50%, is 1; If the electric quantity is less than 50%, it is 0. Because the charging rate of the computer is nanosecond (10^-9), that is, the charging process is: refresh once /10^ -9s.

The computer first selects eight points vertically, from the top down, and then inputs electricity horizontally

A vertical set of transistors used to store values such as the number 8

A horizontal circuit is used to charge the transistor.

storage

  • 1 Charge (turn red)
  • 0 does not charge (does not change color)

read

  • More than 50% is 1
  • Less than 50% is zero

By doing so, memory can store 0s and 1s

3. How do I store numbers

Computers only store zeros and ones, so to store numbers, you need to convert numbers (octal, decimal, hexadecimal) to binary zeros and ones

Decimal –> binary

37(10) == 100101(2) What base does the number in parentheses represent

* means times, and ^ means what power? Y is for unknown, you need to figure it out

37(10) = 3 * 10^1 + 7 * 10^0 = n1 * 2^? + n2 * 2^? = 1 * 2^5 + 1 * 2^2 + 1 * 2^0 = 1 * 2^5 + 0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 1 * 2^0 = 100101(2)Copy the code

– (10) = = 37-100101 (2)

Negative numbers are going to be stored as complement, which is a little bit more complicated, so I won’t go into that

0.75 (10) = = 0.11 (2)

0.75 (10) = 7 + 5 * * 1/10 1/10 ^ 2 = n1 + n2 * * 1/2 1/4 = 1/2 + 0.5 + 0.25 = 1 * 1 * 1/4 = 0.11 (2)Copy the code

Small Tips:

To facilitate writing, binary is usually written in hexadecimal

Binary -> hexadecimal, each four binary represents a hexadecimal number

As an example, the following transformation can be derived from this example:

1111(2) –> F(16)

1111(2) = 2^3 + 2^2 + 2^1 + 2^0 = F(16)
Copy the code
binary hexadecimal
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

Method of base conversion 2

  • The integer

  • The decimal

4. How do I store characters

Use ASCII tables (decimal numbers instead of frequently-occurring symbols and upper and lower case English letters) to store a small number of strings

Number each character

ASCII(American Standard Code for Information Interchange)

The American Standard code for Information Interchange is a computer coding system based on the Latin alphabet. It is mainly used to display modern English, while its extension EASCII can partially support other Western European languages and is equivalent to the international standard ISO/IEC 646.

If you want to store a, you have to store 97(10) in binary

97(10)  = 9 * 10^1 + 7 * 10^0
	= n1 * 2^? + n2 * 2^?
	= 64 + 32 + 1
	= 1 * 2^6 + 1 * 2^5 + 1 * 2^0
	= 01100001(2) == 61(16)
Copy the code

If you want to store A, you have to store 65 of 10 in binary

65(10)  = 6 * 10^1 + 5 * 10^0
	= n1 * 2^? + n2 * 2^?
	= 64 + 1
	= 1 * 2^6  + 1 * 2^0
	= 01000001(2) == 41(16)
Copy the code

The above example shows that the computer considers A and A to be different because the corresponding ASCII characters are different

5. How to store Chinese

Use GBK

  • GB 2312 National Standard simplified Chinese character set
  • GBK character set launched by Microsoft (storage of rare characters, traditional Characters, Japanese, Korean, etc.)
  • GB2312 contains 6,763 Chinese characters in total, and 682 characters including Latin letters, Greek letters, Japanese hiragana and Katakana letters, and Russian Cyrillic letters.

6. How do I store all characters

One thing the Unicode Consortium did in order to make all characters visible on a computer, while considering compatibility issues with different character sets, was to compile all the characters in the world into a single table.

Disadvantages: The original USE of ASCII encoding for storage, only 1 byte (8 bits) is enough, but using Unicode requires 4 bytes (32 bits) for storage; It used to take two bytes (16 bits) to store a Chinese character. With Unicode, it takes four bytes (32 bits) to store a Character. This is a huge waste of storage space.

  • The Unicode character set
  • Unciode numbers global characters, including Chinese, Japanese, Korean, Tibetan, Braille, cuneiform,Performing a word: -), draw text

7. How to store in less space

Low price

a -> 00000000 00000000 00000000 011000012 = 0061(16)

You -> 00000000 00000000 01001111 011000002 = 4F60(16)

Cost-effective UTF-8

a -> 01100001

You -> 11100100 10111101 10100000

Low cost performance, because the computer needs to use 4 bytes of storage

Let me explain your UTF-8

//Unicode you -> 00000000 00000000 01001111 011000002 // UTF-8 you -> 11100100 10111101 10100000 //1110 It tells the computer to read the next three bytes, and each byte starts with a 10, and 10 means I follow, except for meCopy the code

Utf-8 is an encoding, not a character set

Utf-8 encoding mode

Practical problems

  • Why do some Chinese software prefer to use GBK instead of Unicode/UTF-8?

    (1) History of development: A few years before GB 2312 was invented in China, Microsoft’S GBK had not yet appeared, and people were used to GB 2312 and GB13000. However, the newly released Unicode, although unified globally, was not yet perfect and did not adopt UTF-8 encoding method. After the emergence of GBK, the original GB 2312 more optimized, loved by everyone. In 2000, GB 18030-2000 appeared, which is unified and compatible with GBK, but from 1990 to the beginning of 2000, generations are still used to the GBK character set.

    (2) National conditions: At that time, the information was relatively closed, and it was not easy to find anything like Google now, so the character set used by most software in the 1990s was still GBK

  • JavaScript uses the Unicode character set, but does not use UTF-8 encoding

    • JavaScript uses UCS-2 encoding because UTF-16 was not invented in 1995 and JavaScript does not want to use UTF-32
    • What happens :ES5 can’t represent characters after \uFFFF (e.g. \u1D306), there are bugs in some cases,Javascript has a Unicode sinkhole

What is the ASCII, Unicode, UTF-8, GBK relationship?

  1. At first there was only the ASCII character set, because at first only European and American countries could specify computer rules.
  2. The Chinese also need to input Chinese to the computer, so the symbol after ASCII127 is removed and two characters larger than 127 are concatenated to represent a Chinese character, named GBK2312, which can be considered as a Chinese extension of ASCII.
  3. ASCII127 was added, but GBK2312 was not enough, so simply put a Chinese character in the GBK character set after ASCII127
  4. This is where Unicode comes in, which includes all characters. The disadvantage is that whether you are English or Chinese, you must use two bytes (16 zeros and ones) for storage.
  5. At this point, UTF-8 was introduced, which can vary the length of UTF-8 according to different byte lengths. For example, characters in ASCII are represented by one byte (byte: eight zeros or ones for a character), and Chinese characters are represented by three bytes

Expand & Think:

  • What is the difference between encoding mode and character set? Encoding mode? Character set?
  • How do I store Unicode on a computer
  • Hexadecimal, Unicode, UTF-8 differences
    • Unicode (which is hexadecimal) is the efficient UTF-8 (also hexadecimal) storage method.
  • Negative numbers are stored as complement
  • Decimals are stored as floating point numbers

If there are any mistakes in this article, please correct them in the comments section. If this article helped you or you liked it, please like it and follow it.