zoukankan      html  css  js  c++  java
  • 2.3 Integer Arithmetic

    Adding two positive integers can yield a negative result, and the comparation x<y can yield a different result with the comparation x-y<0. This properties are caused by the finite nature of computer arithmetic.

    2.3.1 Unsigned Addition

    Consider two nonnegative integers x and y, which are represented by w-bit unsigned numbers , s.t. .

    Thus, , which need w+1 bits to represent the sum of x+y.

    If we maintain the sum of x+y as w+1 bits, and add it to another value, we may need w+2 bits, and so on. So this infinite word size bits can not fully represent the results of this arithmetic operation. Note that, some other languages,as lisp can support infinite precision arithmetic.

    In general, if , the leading bit in w+1-bit representation of the sum will be 0, and hence discarding it will not change the numeric value. While if , the leading bit will be 1, and hence discarding it will be equivalent to substracting from the sum.

    Let define the operation for the arguments x and y such that , so

    image This is precisely the result in C when adding two w-bit unsigned values.

    In C programs overflows are not signaled as errors, The following method can determine whether overflow has occurred.

    Supposed , we claim overflow occurrs when s<x, or equivalent s<y.

    To see this clearly, if s did not overflow, we have surely ,  on the other hand, if s did oveflow we have .

    Let us define the value such that ,where is an operation.

    image

    2.3.2 Two’s-Complement Addition

    We can not use the modular addition for two's-complement addition. While most computers use the same instructions to perform either signed and unsigned addition.

    Thus, we can define two's-complement addition for ,denoted as on operands x and y such that as

    image

    By equation 2.5, we can write , and , and use the property that is simply addition modulo , then we have following derivation:

    image The following figure illustrates this formula:image

    That is:

    image This model is just from mathemetic sight. When the computer works, it actually  truncates for w bits int particular data type size.

    For the following example,

    image

    Equation also let us identify the cases where overflow has occurred. when both x and y are negative but xy>=0, we have negative overflow. when both x and y are positive but xy<0, we have positive overflow.

    2.3.3 Two’s-Complement Negation

    For every number x in the range has a additive inverse under :

    Define the value such that as

    image

    Bit-level representation of two’s-complement negation

    1.One technique for performing two’s-complement negation at the bit level is to complement the bits and then increment the result. That is compute the –x and ~x+1 will give identical results. ( T2B(x) + T2B(~x+1) = 0  )

    2. Another way is based on spiting the vector into two parts.

        Let k be k-th position from rightmost, where first meet with the 1, so the bit-level representation of x has the form ,the negation is written in binary form .      ( + = 0).

    2.3.4 Usnsigned Multiplication

    Define be the w-bit unsigned multiplication operation.

    image 

    2.3.5 Two’s-Complement Multiplication

    Define be the w-bit unsigned multiplication operation.

    image

    We claim that the bit-level representation of the product operation is identical for both unsigned and two’s-complement multiplication.

  • 相关阅读:
    第9.11节 Python中IO模块文件打开读写操作实例
    第9.10节 Python中IO模块其他文件操作属性和方法简介
    Python使用import导入模块时执行了模块的文件但报ModuleNotFoundError错误的愚蠢问题
    Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案
    第9.9节 Python文件随机读写定位操作方法seek
    第9.8节 Python使用writelines函数写入文件内容
    第9.7节 Python使用write函数写入文件内容
    Python中import模块时报SyntaxError: (unicode error)utf-8 codec can not decode 错误的解决办法
    The 2nd tip of DB Query Analyzer
    水仙花数优化问题:穷举法、查找表法、组合数学法
  • 原文地址:https://www.cnblogs.com/baiweiguo/p/2863000.html
Copyright © 2011-2022 走看看