CSAPP 2-2 integer representation and calculation

table of Contents

1 integer

There are two integer encoding: one can only represent non-negative number (a number greater than 0), another can represent negative, zero, and positive.

(1) integer data type:

C and C ++ support signed (the default) and unsigned, Java support only a signed number.

(2) unsigned code:

Unsigned binary representation of another very important attribute:

Each between 0 ~ 2 ^ w - has a unique number between 1 and a value of w bits coding, and vice versa.

(3) complement encoding:

The most common have signed a computer representation that complement (two's-complement) form: most-significant-bit word as a negative right (negative weight), which is the sign bit.

Like an unsigned representation, each number within the range can be represented by a unique complement of the bit coded w - Uniqueness of complement encoding.

There are two attention to detail:

A) the scope of complement is asymmetric: | TMin | = | Tmax | + 1, i.e. TMin no corresponding positive - negative number indicates a general pattern of bits (the sign bit is a number), the other. half represents a non-negative number, and 0 is non-negative, it can be represented by a positive number less than negative one.

b) maximum unsigned value than 2 times the maximum value just complement large 1: UMax = 2TMax + 1.

- there is a point Note: maximum UMax -1 complement and unsigned bit representation of the same - through a full 1, while a value of 0 in both string representation are all zero.

C language standard does not require the use complement form to represent a signed integer, but almost all of the machines are doing.
So for portability program, developers do not manually change represents a range of values, and do not assume that signed will use other special representation.

  • Extended: disassembler is an executable program file is converted back to a more readable ASCII codes of these files contain many hexadecimal numbers, all with typical complement form to represent these values .
    can recognize numbers generated by the disassembler and understand their meaning, is an important skill.

(4) with a converter between the number and the unsigned number notation:

C language allows casts made between various types of digital data.

Achieve the most C language, the process has the same word length general rule conversion between symbols and unsigned numbers: The numbers may change, the value stored in the bit pattern does not become low level .

Signed and unsigned numbers (5) C language

Most digital by default is signed, to create an unsigned number, you must add the suffix character 'U' or 'u', for example: 12345U, or 0x1A2Bu.

C language, as with printfthe output value, respectively, with the indicator %对, %uand %xrepresents a signed decimal, unsigned decimal and hexadecimal form a digit.

To be continued ...

Guess you like

Origin www.cnblogs.com/shoufeng/p/11524987.html