Base conversion (background supplement to complete Python14 homework)

From the small turtle hexadecimal conversion video

Number recognition

  • Number is an intangible and abstract concept, it is a product of human intelligence used to express quantity
  • "Hundred" is an abstract concept, it becomes a concrete and tangible concept by counting the number of pages in a book
  • We have to realize: 100 is not necessarily one hundred, and the expression of one hundred is not just 100
100 decimal representation
C Roman numeral representation
6416 base 16/hexadecimal representation
11001002 base two/binary representation
1448 base eight/octal representation
one hundred English presentation

Counting system

  • The counting system is a mechanism used by humans to express values.
  • In today's society, people most commonly use the decimal number system (base 10), while most computer systems use binary notation.
  • If you confuse the two will lead to poor code, so in order to write excellent code, we must eliminate this confusion
  • How is 123.45 expressed? ? ?
    123.45=1✖100+2✖10+3✖1+4✖10 -1 +5✖10 -2 =100+20+3+0.4+0.05

Conversion between decimal and binary representations

In order to take care of human habits, computers must convert between the decimal notation used by humans and the binary format used by the computer itself.

Binary → Decimal

1100 1010(2) = 1✖27+1✖26+0✖25+0✖24+1✖23+0✖22+1✖21+0✖20=128+64+8+2=202(10)

Decimal → Binary

202(10)
202 ➗ 2 = 101 …… 0 (The remainder of the first division is the ones place, so the result is from top to bottom)
101 ➗ 2 = 50 …… 1
50 ➗ 2 = 25 …… 0
25 ➗ 2 = 12 …… 1
12 ➗ 2 = 6 …… 0
6 ➗ 2 = 3 …… 0
3 ➗ 2 = 1 …… 1
1 ➗ 2 = 0 …… 1 The
result from top to bottom is 1100 1010

Hexadecimal counting system

Because the binary system is too verbose, it is particularly troublesome and error-prone for reading and writing, so programmers usually avoid using binary notation directly in program source files. They choose to use hexadecimal instead of binary.

Why use hexadecimal instead of decimal? ? ?

  • Hexadecimal is very compact
  • The conversion between binary and hexadecimal is very easy

Representation and conversion of hexadecimal numbers

  • What should I do if the number is not enough? ? ?
  • 0、1、2、3、4、5、6、7、8、9、A、B、C、D、E、F

Hexadecimal → Decimal

1234(16) = 1 ✖ 163+2 ✖ 162+ 3 ✖ 161 + 4 ✖ 160= 4096 + 512 + 48 +4 =4660(10)

Hexadecimal → Binary

Binary Hexadecimal
0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

1011 1101 1010 1010 (2) = BDAA (16)

Octal counting system

  • Octal notation is very common in early computer systems, but it is already OUT for our current computer systems
  • The octal system is mainly suitable for 12, 36-bit computer systems at the time (or other computer systems with multiples of 3)
  • For computer systems where the number of digits is a power of 2 (8-bit, 16-bit, 32-bit or 64-bit computer systems), the octal representation seems out of place

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/113731063