Representation and processing of information

Source: Internet
Author: User

P20

Unsigned number: The encoding is based on the traditional binary notation (numbers greater than or equal to zero).

Complement: Encoding is the most common way to represent signed integers (numbers that are positive or negative).

Floating-point number: The encoding is a two-base version of the scientific notation for real numbers.

Although the representation of an integer can only encode a relatively small range of values, it is accurate, while floating-point numbers may encode a larger range of values, but this representation is an approximate representation.

A large number of computer security vulnerabilities are caused by the subtle details of computer operations.

P22

Computers use most of the 8-bit blocks, or bytes, as the smallest addressable storage unit, rather than accessing individual bits in memory.

Hexadecimal notation:

Take the binary to make intermediate results.

Word:

Each computer has a word length that indicates the nominal size of integers and pointers.

The most important system parameter that the byte determines is the maximum size of the virtual address space.

For a machine with a word length of W, the virtual address range 0~2* (w-1), the program accesses a maximum of 2*w bytes (common w=32 or 64)

Data size:

Computers and compilers support many different encoded numeric formats, such as integers and floating-point numbers, and other lengths. For example, many machines have instructions for handling a single byte, as well as Integer instructions that represent 2-byte or 4-byte or 8-byte, while others support floating-point numbers expressed as 4-byte and 8-byte.

The gcc-m32 can generate 32-bit code on a 64-bit machine.

P26

Addressing and byte order:

For program objects that span bytes, we must create two rules: what is the address of this object, and how this byte is arranged in memory.

The order of bytes is the basis of network programming, the small end is: "High-to-high, low-to-low", the big-endian opposite.

P28

P32

Logical operation (the result is 0 or 1):

and:&&

Or:| |

Non -:!

1 indicates ture 0 indicates false

Bit arithmetic (the result is a bit vector):

With: &

Or: |

Non: ~

XOR: ^

Bitwise operations

P33

The Mask is an important application of bit operations, and bit-specific positioning can be set to 0 or one (to selectively mask the signal)

P38

Integer data type:

An integer that represents a finite range.

Each type can specify a size with a keyword, including char or short or long or long long, as well as a non-negative number (unsigned) or possibly a negative number (default).

The number of words for these different sizes will vary depending on the length of the machine and the compiler.

Use the "Long Long" type in C99. Compiling is to use Gcc-std=c99

P39

Unsigned number encoding:

For each bit vector of length w, there is a unique value corresponding to it; in turn, each integer between 0~2*w-1 has a unique binary with a bit vector length of W.

Complement code:

For each bit vector with a length of W, there is a unique value corresponding to it; in turn, each integer between -2* (w-1) and w-1-1 has a unique bit-vector binary corresponding to the length of W.

P44

Signed and unsigned conversions:

Signed and unsigned rules, bit vectors unchanged

Bit + context

P48

Negative number of the computer is the inverse of the corresponding positive number +1

P49

0 Extensions:

To convert an unsigned number to a larger data type, we simply add 0 at the beginning of the representation.

Symbol Extensions:

Converts a complement number to a larger data type, where the rule is to add a copy of the most significant bit in the representation

P52

As we can see, the implicit coercion type conversion of a signed number to an unsigned number results in some non-intuitive behavior. These non-intuitive features often lead to program errors, and this kind of subtle difference error that includes implicit coercion type conversions is difficult to find, because this type of coercion is not explicitly indicated in the code.

such as exercises 2.25

Length is natural to pass as an unsigned number, and i<=length-1 is natural, but together, the code tries to access the illegal elements of array A.

P54

Overflow:

in unsigned arithmetic operations, there is no so-called "overflow" that says: All unsigned operations are modeled by the N-square of 2, where n is the number of digits in the result. If one operand of an arithmetic operator is a signed integer and the other is an unsigned integer, then the signed integer is converted to an unsigned integer, and "overflow" cannot occur. However, when two operands are signed, the overflow can occur, and the result of the overflow is undefined. When the result of an operation overflows, it is unsafe to make any assumptions.

Avoid:

The right approach is to rely on a good definition of unsigned arithmetic, that is, to convert between signed and unsigned:

"Excerpt from: http://blog.csdn.net/milan25429688/article/details/328944#4.8"

P67

The last thoughts on integer arithmetic:

A computer performs an integer operation that is actually a form of modulo operation.

Whether the operands are represented in unsigned or complement form, there are exactly the same or very similar bit-level behavior

Floating point number:

A floating-point representation encodes a rational number such as V=xx2*y

It involves a very large number of executions (| V|>>0) very close to 0 (| v|<<1) numbers, and more generally as calculations of approximate values of real numbers, are useful

Floating-point numbers have a scientific basis on which to understand, IEEE standards

P68

Ieee:

Institute of Electrical and Electronics Engineers (IEEE) American Society of Electrical and Electronics Engineers

IEEE is authorized by the International Organization for Standardization as a standard-setting organization, with a dedicated standard working committee, with 30000 of volunteer workers involved in standard research and development, and annual development and revision of more than 800 technical standards.

IEEE Standard-setting content includes: Electrical and electronic equipment, test methods, original devices, symbols, definitions and test methods.

Uncertainty and rounding of floating-point arithmetic:

1. Rounding to even

2. Rounding to 0

3. Rounding Down

4. Rounding up

P70

The IEEE floating-point standard uses v= ( -1) *s XMX-E to represent a number:

Symbol: s Determines whether the number is negative (s=0) or positive (S=1). And for the 0 sign bit special case processing.

Mantissa:m is a binary decimal with a range of 1~2-ε or 0~1-ε ( n-th power of Ε=1/2)

Order code:E to the floating-point weighting, the weight is 2 of the e-power (may be negative)

Encoding Rules:

Float

The S field is 1 bits, the exp fields are k=8 bits, the frac is n=23 bit, and a 32-bit representation is obtained.

Double

The S field is 1 bits, the exp fields are k=11 bits, the frac is n=52 bit, and a 64-bit representation is obtained.

P74

Understand conversion rules for integers and floating-point numbers

IEEE 754 also has special provisions for the effective number M and index e:

(1) E is not all 0 or not all 1:
The floating-point number then uses the above rule to indicate that the calculated value of the exponent e minus 127 (or 1023) to get the real value and then the effective digit m plus the first digit of 1.

(2) E is all 0:
At this point the index e of the floating-point number equals 1-127 (or 1-1023) the effective digit m no longer adds the first bit of 1 but reverts to the decimal of 0.xxxxxx. This is done to represent ±0
and a very small number close to 0.

(3) E is all 1:
At this point if the effective number m is all 0 ± infinity (plus or minus depending on the sign bit s) if the valid number m is not all 0 means that the number is not a number (NaN).

Summary:

When casting conversions between unsigned and signed integers of the same length, most C-language implementations adhere to the principle that the underlying bit pattern remains unchanged. On the complement machine, for a W-bit value, this behavior is described by the function T2uw and the function U2TW. The C-language implicit coercion type conversion can result in many programmers ' unpredictable results, often leading to errors in the program.

Because of the limited length of the encoding, computer operations have completely different properties than traditional integers and real numbers. A finite length can cause a numerical overflow when the range of the representation is exceeded.

We have to be very careful with floating-point operations, because floating-point operations have limited range and precision, and do not follow ordinary arithmetic attributes, such as binding.

Representation 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.