系统级程序设计 - chapter2

chapter2目录

  • Representing information as bits
  • Bit - level manipulations
    • Boolean Algebra (布尔算法)
    • Example: Representing & Manipulating Sets
      • Representation
      • Operations
    • Contrast: Logic Operations in C
    • Shift Operations(移动操作)
  • Integers
    • Representation: unsigned and signed
      • Numeric Ranges
    • Conversion, casting
      • Mapping Signed & Unsigned
      • Conversion Visualized
      • Casting Surprises
      • Summary
    • Expanding, truncating
    • Addition, negation, multiplication, shifting
      • Unsigned Addition
      • Multiplication
    • Summary
      • Addition:
      • Multiplication:
  • Representations in memry, pointer, strings
    • Machine Words
    • Byte Ordering
    • Complement & Increment Examples

Representing information as bits

  • Each bitis 0 or 1(每位是0或1)

  • By encoding/interpreting sets of bits in various ways
    – Computers determine what to do (instructions)
    – … and represent and manipulate numbers, sets, strings, etc…

  • Why bits? Electronic Implementation
    – Easy to store with bistable elements(双稳态成分)
    – Reliably transmitted on noisy and inaccurate wires
    系统级程序设计 - chapter2_第1张图片

Bit - level manipulations

Boolean Algebra (布尔算法)

系统级程序设计 - chapter2_第2张图片

Example: Representing & Manipulating Sets

Representation

– Width w bit vector represents subsets of {0, …, w–1} 序号从0开始然后从右开始
– aj = 1 if j ∈ A

  • 01101001 {0,3,5,6}
    76543210
  • 01010101 {0,2,4,6}
    76543210

Operations

  • & Intersection
    – 01000001 { 0, 6 }
  • | Union
    – 01111101 {0,2,3,4,5,6}
  • ^ Symmetric difference
    – 00111100 { 2, 3, 4, 5 }
  • ~ Complement
    – 10101010 { 1, 3, 5, 7 }

Contrast: Logic Operations in C

Contrast to Logical Operators

  • &&, ||, ! (两个表示
    – View 0 as “False”
    – Anything nonzero as “True” (非0都当作正确)
    – Always return 0 or 1
    Early termination(早结束)

Shift Operations(移动操作)

Left Shift: x << y

  • Shift bit-vector x left y positions
    –- Throw away extra bits on left and fill with 0’s on right(丢丢丢全丢掉)
    Right Shift: x >> y
  • Shift bit-vector x right y positions
    – Throw away extra bits on right
  • Logical shift 逻辑移动
    – Fill with 0’s on left
  • Arithmetic shift 算数移动
    – Replicate most significant bit on left  ---- Undefined Behavior
    – Shift amount < 0 or ≥ word size

Integers

Representation: unsigned and signed

Numeric Ranges

Unsigned Values
无符号的讨论

  • UMin = 0
    – 000…0
  • UMax = 2w – 1
    –111…1
    Two’s Complement Values
  • TMin = -2w-1
  • 10…0
  • TMax = 2w-1-1
    – 011…1

Values for W = 16
系统级程序设计 - chapter2_第3张图片

Conversion, casting

Mapping Signed & Unsigned

系统级程序设计 - chapter2_第4张图片

Conversion Visualized

系统级程序设计 - chapter2_第5张图片

Casting Surprises

  • Expression Evaluation
    – If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned
  • Including comparison operations <,>,==,<=,>=
  • Examples for W = 32:
    – TMIN = -2,147,483,648
    – TMAX = 2,147,483,647
    系统级程序设计 - chapter2_第6张图片

Summary

Casting Signed ↔ Unsigned: Basic
Rules

  • Bit pattern is maintained
  • But reinterpreted
  • Can have unexpected effects: adding or subtracting 2w
  • Expression containing signed and unsigned int
    – int is cast to unsigned!!

Expanding, truncating

Task:

  • Given w-bit signed integer x
  • Convert it to w+k-bit integer with same value
    Expanding, Truncating: Basic Rules - Expanding (e.g., short int to int)
    – Unsigned: zeros added
    – Signed: sign extension
    – Both yield expected result
  • Truncating (e.g., unsigned to unsigned short)
    – Unsigned/signed: bits are truncated
    – Result reinterpreted
    – Unsigned: mod operation
    – Signed: similar to mod
    – For small numbers yields expected behavior

Addition, negation, multiplication, shifting

Unsigned Addition

Multiplication

  • Goal: Computing Product of w-bit numbers x, y
    – Either signed or unsigned
  • But, exact results can be bigger than w bits
    – Unsigned: up to 2w bits
    Result range:0≤x * y≤(2w –1)2 = 22w –2w+1 +1
    – Two’s complement min (negative): Up to 2w-1 bits
    Result range: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1
    – Two’s complement max (positive): Up to 2w bits, but only for (TMinw)2
    Result range: x * y ≤ (–2w–1) 2 = 22w–2

Summary

Addition:

Unsigned/signed: Normal addition followed by truncate, same operation on bit level

  • Unsigned: addition mod 2w
    – Mathematical addition + possible subtraction of 2w
  • Signed: modified addition mod 2w (result in proper range)
    – Mathematical addition + possible addition or subtraction of 2w

Multiplication:

  • Unsigned/signed: Normal multiplication followed by truncate, same operation on bit level
  • Unsigned: multiplication mod 2w
  • Signed: modified multiplication mod 2w (result in proper range)

Representations in memry, pointer, strings

Machine Words

Any given computer has a “Word Size”

Byte Ordering

Example

  • Variable x has 4-byte value of 0x01234567
  • Address given by &xis 0x100
    大端地址
    – 大端地址放低字节(低位)
    小端地址
    – 小端地址放低字节(低位)
    系统级程序设计 - chapter2_第7张图片

Complement & Increment Examples

系统级程序设计 - chapter2_第8张图片

你可能感兴趣的:(系统级程序设计)