Integer expression complement and unsigned addition and multiplication Csapp learning notes

Source: Internet
Author: User

The computer uses bits to represent integers, one way to represent only non-negative numbers, and one that can represent signed numbers.

Unsigned number encoding:

Complement code:

From the definition above you can know the correspondence between the complement and the unsigned (see below), the highest bit is 0 o'clock, the complement is the same as the unsigned representation, and the highest level is 1 o'clock, for example, the complement of 1 corresponds to the unsigned number of 4294967295.

You must first understand the expansion and truncation of integers before integer operations

extension is divided into 0 extension and symbol extension, 0 extension is simple to add 0 at the beginning of the representation, applicable to the expansion of unsigned number. The symbol extension adds a copy of the most significant bit value in the representation, which applies to the complement extension. For example, the 4-bit 1101 expands to 8 bits, or 11111101, and it is easy to prove that the expansion of the unsigned bit does not change its value.

Truncation is simply to discard the higher bits, and here we take the truncation with the "modulo" (MoD) mentioned below as one thing. To give a complement to the truncation of the number, 11101111 truncated to 4-bit value, the result is 1111 namely-1 (here also need to convert the bit representation to an unsigned number), here truncated to 4 in mod (16) equivalent (if the individual understanding is wrong, also please crossing point).

PS: There is a difference between the MoD and C + +, the division in C + + is closer to 0, for example, -7%4=-3.

The sum of the W-complement of two numbers of integer additions is expressed in the same bit level as the unsigned sum.

unsigned addition, As the number of two numbers added may increase the number of representation bits, which can cause the word length to swell, in the general programming language, only support the fixed precision operation. When two unsigned numbers are added to overflow (two W-bits add up to the value of the w+1 bit), the highest bit is specified, equivalent to the calculation and modulo 2^w.

complement addition , complement addition is slightly more complex, and the complement method is to convert two numbers to unsigned numbers, add them (using the rules of Unsigned addition) and then convert them back.

 

In fact, we usually in the calculation as long as the formula can be used, do not need to do the calculation every time to go through the whole process, such as the first transformation in the addition of the mold in the conversion, ah, too annoying.

integer multiplication the bit-level representation of complement and unsigned multiplication , and finally know why the use of complement, the machine with the same multiplication instruction to calculate the multiplication, for addition is the same

unsigned multiplication , fixed number of bits, so directly to the multiplication results modulo, when the actual calculation of two 32-bit length of integers, the results of the calculation is stored in two registers, the final result is one of the register value (low 32 bits).

The complement multiplication , first obtains the product of two numbers (that is, the product of the number represented by the complement, such as -1*7=-7), then truncates, and then converts to the signed number. The calculated formula is as follows

Now let's analyze why the bitwise representation of the multiplication is the same, that is, why the low lows of the unsigned and complement product are the same. Because for the same two bit patterns, the product of the unsigned value represented is the same as the result of the complementary product modulo, with X-apostrophes, y-apostrophes representing unsigned values, and X, Y for the complement value, there is

The 2^w are discarded, so the low is the same. The meaning of this equation is that the bit-level representation of the unsigned product value indicates that a sequence of operations can be performed to obtain a bit-level representation of the complement deposition value, but this sequence of operations is only relevant to the high (greater than or equal to w+1) and the final result is only related to the low.

Integer expression complement and unsigned addition and multiplication Csapp learning notes

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.