Chap 2 Representing and Manipulating Information (CS: APP)

Source: Internet
Author: User
Tags truncated

Bytes --------------------------------------------------------------------------------------------------

Author: YuManZI

1.1-3.6

To Be Continued.

Bytes --------------------------------------------------------------------------------------------------

1. Information Storage

1.1 Virtual Memory: A machine-level program views memory as a very large array of bytes.

1.2 Data Sizes(Bytes)

32 bit: char 1; short [int] 2; int 4; long [int] 4; long [int] 8; char * (any pointer) 4; float 4; double 8;

64 bit: char 1; short [int] 2; int 4; long [int] 8; long [int] 8; char * (any pointer) 8; float 4; double 8;

Contents within square brackets [] are optional. The main difference between 32 bit and 64 bit machines are following two points: a) different sizes of data type long; B) different sizes of pointers.

1.3 Byte Ordering: Large endian & little endian. (Take 0x01234567 as example .)

Big endian, the most significant byte comes first (lower address), bytes from low address to high address are 01 23 45 67, respectively;

Little endian, the least significant byte comes first (lower address), bytes from low address to high address are 67 45 23 01, respectively.

1.4 Shift Operations in C

Left shift <k: dropping off the k most significant bits and filling the right end with k zero;

Logical right shift> k: dropping off the k least significant bits and filling the lest end with k zero;

Arithmetic right shift> k: dropping off the k least significant bits and filling the lest end with kMost significant bit.

Arithmetic right shift uses the most significant bit as filling unit, because of the two's complement representation of negative integers. in some languages ages (e.g. java), the number of shifting bits can never be more than bit sizes of data types.


2. Integral Data Types

2.1 Unsigned Encodings(W bits,X= [X _ (W-1), x _ (W-2),..., x_0])

B2U (X) = Sigma {I = [0, W-1]} (x_ I * 2 ^ I)

B2U means Bits to Unsigned

It can represent integers between [0 .. 2 ^ W-1]

2.2 Two's Complement Encodings(Same setting as 2.1)

B2T (X) =-X _ (W-1) * 2 ^ (W-1) + sigma {I = [0, W-2]} (x_ I * 2 ^ I)

B2T means Bits to Two's

It's a signed encoding, can represent integers between [-2 ^ (W-1), 2 ^ (W-1)-1], the difference between it and Unsigned encoddings are the weight of the significant bit, I. e. positive for unsigned and negative for two's complement.

2.3 Conversions

Signed <--> Unsigned with identical size: the key is to keep bit representation stable;

Large size-> Small size with same type of signed or unsigned: truncate directly;

Small size-> Large size with same type of signed or unsigned: fill the significant bit at left end;

Large size-> Small size with different types of signed and unsigned, respectively: transfer to small size according to rule 2 first and then convert according rule 1.

Small size-> Large size with different types of signed and unsigned, respectively: transfer to large size according to rule 3 first and then convert according rule 1.

2.4 Expanding the Bit Representation of a Number, Two points:

A) Numbers will be regarded as a signed integers;

B) if an expression involves both types (I. e. signed and unsigned), all operands will be converted to unsigned first, followed by computing them.

2.5 Advice on Signed and Unsigned

A mixing use of signed data and unsigned data may cause some subtle errors.Always using signed dataIs a good habit. Indeed, some languages ages (e.g. java) do not support unsigned data types, as they think the benefits offering by signed data types are less than the dangers they may introduce.


3. Integer Arithmetic

3.1 Unsigned Addition(2 w-bits unsigned int x & y)

X + y = B2U (U2B (x) + U2B (y ))

Overflow: x + y> = 2 ^ w, then sum = x + y-2 ^ w.

Result on (w + 1) th bit will be discarded. Overflow flag: sum <x & sum <y

3.2 Two's-Complement Addition(2 w-bits signed int x & y)

Principle:Add x and y as adding two bit vectors, and interpret the truncated result as signed int.

X + y = B2T (T2B (x) + T2B (y) = U2T (T2U (x) + T2U (y ))

Three conditions:

Negative overflow:-2 ^ (W-1) <= x + y <= 2 ^ (W-1)-1, then sum = x + y + 2 ^ w;

Normal:-2 ^ (W-1) <= x + y <2 ^ (W-1), then sum = x + y;

Positive overflow: 2 ^ (W-1) <= x + y <= 2 ^ w-2, then sum = x + y-2 ^ w.

Discussion:The bit-level representation of addition operation is identical for both unsigned and two's-complement addition, but different interpretation of the result.

3.3 Two's-Complement Negation

For w-bits signed type, representable integers are within [-2 ^ (W-1), 2 ^ (W-1)-1]. when x =-2 ^ (W-1),-x =-2 ^ (W-1) = x(Nonintuitive);

While for other x \ in [-2 ^ (W-1) + 1, 2 ^ (W-1)-1],-x =-x.

There are other vulnerabilities caused by the asypolicric bounds of signed data types.

Bit-level representation of two's-complement negation:Complement the bits and then increment the result.

3.4 Unsigned Multiplication(2 w-bits unsigned int x & y)

Overflow: x * y> = 2 ^ w, then mul = (x * y) % 2 ^ w.

3.5 Two's-Complement Multiplication

Principle:Multiple x and y as two bit vectors, and interpret the truncated result as signed int.

X * y = B2T (T2B (x) * T2B (y) % 2 ^ w) = U2T (T2U (x) * T2U (y )) % 2 ^ w)

Discussion:The bit-level representation of product operation is identical for both unsigned and two's-complement multiplication, but different interpretation of the result.

3.6 Conclusion on Integer Arithmetic

A) regard subtraction operation as a combination of negation and addition;

B) the addition, subtraction and multiplication operations on unsigned arithmetic have the exact same effect as addition, subtraction and multiplication on two's-complement at the bit level, respectively.Simply compute at bit level and interpret the truncated result according to the specific encoding.

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.