[Csapp notes] [Chapter II Presentation and processing of information]

Source: Internet
Author: User

Representation and processing of information 2.1 information storage
    • Machine-level programs treat memory as a very large array of bytes, called Virtual storage .
    • Each byte of the memory is represented by a unique number, called its address
    • A collection of all possible addresses is called a virtual address space
2.1.16 Binary notation

The hexadecimal transformation method in Python

a=15;print(hex(a))//print:0xf
2.1.2 words
    • Length determines the maximum size of the virtual address space
    • 32 bits of 4G (2^32)
2.1.3 Data size
    • char *The full word length of the machine is used
2.1.4 Addressing and byte order
    • Small End Method : Most Intel compliant rules (now most commonly used)

      • High-level address
    • The big- endian approach : IBM and Sun Microsystems

      • High-level low address
    • printf ("%.2x") parsing

      Excerpt from the K&r C programming language:
      Dot, the term separates field width and precision
      Represents the number of precision. For a string, it specifies the maximum number of characters to print, and for E, E, or F conversions, it refers to the
      The number of digits after the decimal point for the print, which specifies the number of significant digits to print for the G or G conversion, and for integers, it specifies the number of digits to print (if necessary, a bit 0 is populated to achieve the required width).

2.1.5 Represents a String
    • Any system that uses ASCII code as a character code will represent the same result, regardless of the byte order and word size rules. Thus, text data has greater platform independence than binary data

    • Java uses Unicode to represent strings. Unicode-enabled libraries are also available for C languages

2.1.6 represents code

Even the same piece of code that uses different, incompatible instructions in different machine types, so the binary code is incompatible.

Introduction to 2.1.7 Boolean algebra

Very simple

    • Bit vectors are often used to describe a set (state compression is commonly used)
Bit-level arithmetic of 2.1.8 C language

ACM play a lot of, don't say

Logical operation of 2.1.9 C language
    • ||&&Lazy Evaluation
Shift operation of 2.1.10 C language
    • Logical Right Shift

      • Go to the left and fill K 0.
    • Arithmetic Right Shift

      • To the left, fill the value of K's most significant bit
      • Have a magical effect on signed operations

Potential problems

    • For unsigned data, the right shift must be logical.
    • For signed data, logic and arithmetic are all possible.
      • Almost all compilers right shift to signed use arithmetic
      • Many programmers also assume that using arithmetic to move right
      • Potential compatibility issues may exist
2.2 integer representing 2.2.1 integer data type
    • have symbols
    • No sign
    • A negative number is larger than the positive number (there is a 0 that is represented as the smallest negative number)
2.2.2 Encoding for unsigned numbers
    • Nature: Double Shot
2.2.3 Complement code

To understand the complement in this way is a new and good idea.

    • It's easy to figure out the range. Turn into the original code without thinking.
    • Double shot, rules no special case, it is easy to see that 0 can not express two.
Complement, anti-code

2.2.5 signed and unsigned numbers in C language
    • The default is signed, if you need an unsigned tail +u, for example:12345U
2.2.6 extends the bit representation of a number
    • Convert to a larger data type for an unsigned number
      • Simply add 0 to the beginning of the representation, which is called the 0 extension
    • For signed numbers, that is, the complement

      • Symbol extension (sign extension), which is the value of adding the most significant bit
    • Contrast logical right SHIFT, and arithmetic right shift

2.2.7 truncating numbers
    • Truncate a number to K-bit, that is, to the high w-k.

    • For unsigned number truncation x it to K-bit results in equivalent

      • B2U ([xk-1,xk-2,.... x0]) =b2u ([xw-1,xw-2...x0]) mod 2^k
    • For a signed number x.

      • When truncated, it looks like an unsigned number.
      • B2T ([xk-1,xk-2,.... x0]) =u2t (B2U ([xw-1,xw-2...x0]) mod 2^k
        )
Suggestions
    • Use signed integers in most cases
      • Only signed integers are supported in Java, >> arithmetic right shifts, >>> logical right Shift
2.3 Integer Operation 2.3.1 Unsigned addition
    • x+y= (x+y) MoD2^k
    • Overflow will be shed.

    • Abelian group, group theory.

2.3.2 Complement addition
    • Most computers use the same machine instructions to perform signed and unsigned sum.

    • X (signed +) y= u2t (t2u (x+y) mod 2^w)

        1. Calculate X+y First
        1. Converts x+y to unsigned type Z.
        1. P=z MoD 2^w
      • Attached. (or simply truncate the binary representation of the x+y to get P)
        1. To represent P with a signed type
    • Positive overflow, normal, negative overflow.

2.3.3 complement of non-
    • Because of the inconsistency of positive and negative intervals
    • So the inverse of the smallest negative number is the smallest negative.

    • For any integer x . -xand ~x+1 The results are exactly the same.

2.3.4 unsigned multiplication
    • Take only the values represented by the low w bits, and the remaining truncation
2.3.5 Complement multiplication
    • For unsigned and complement multiplication, the bit-level representation of the multiplication is the same as the same instruction.

    • The low w bits of the two numbers multiplied by the unsigned and complement are always equal . Proof to see the book.

2.3.6 Multiplication constant
    • Because the multiplication speed is too slow, the machine may use (addition, subtraction, shift) instead of multiplication.

2.3.7 divided by a power of 2
    • For unsigned types or integers, direct right-shift does not have any problems.

    • For negative numbers, the last value is-48.3, which is rounded to 49 instead of-48.

      • Workaround is偏置
      • Principle:
      • C language

        (x<0 ? (x+(1<<k)-1):x)>>k等价于x/pwr2k ,pwr2k=2^k
2.4 Floating point

After that, let's go through something else.

[Csapp notes] [Chapter II Presentation and processing of information]

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.