目录
1.Representing information as bits
(1)Representation: unsigned and signed
(4)Addition, negation, multiplication, shifting
4.Representations in memory, pointers, strings
1.Representing information as bits
p3-p4 | Everything is bits ---Each bit is 0 or 1 ---By encoding/interpre6ng sets of bits in various ways ---Why bits? Electronic Implementa6on For example, can count in binary ---Base 2 Number Representa6on |
p5-p6 | Encoding Byte Values ---Byte = 8 bits Example Data Representations |
2.Bit-level manipulations
p8-p10 | Boolean Algebra ---And、Or、Not、Exclusive-Or General Boolean Algebras ---Operate on Bit Vectors & | ^ ~ ---All of the Proper6es of Boolean Algebra Apply Example: Represen6ng & Manipulating Sets ---Operations |
p11 | Bit-Level Operations in C ---Examples (Char data type) |
p12-p13 | Contrast: Logic Opera6ons in C ---Examples (char data type) &&, ||, ! |
p14 | Shift Operations ---Right Shift: x >> y ---Undefined Behavior |
3.Integers
(1)Representation: unsigned and signed
p16 | Encoding Integers 注:有很多表示signed的方法,这门课我们只关注补码形式 ---C short 2 bytes long ---Sign Bit |
p17 | Two-complement Encoding Example (Cont.) |
p18 | Numeric Ranges ---Two’s Complement Values ---Other Values |
p19 | Values for Different Word Sizes ---C Programming |
p20 | Unsigned & Signed Numeric Values ---Uniqueness ---Can Invert Mappings |
(2)Conversion, casting
p22 | Mapping Between Signed & Unsigned ---Keep bit representations and reinterpret |
p23-p24 | Mapping Signed ↔ Unsigned |
p26 | Conversion Visualized ---2’s Comp. → Unsigned |
p27 | Signed vs. Unsigned in C ---Casting |
p28 | Casting Surprises |
p29 | Summary:Casting Signed ↔ Unsigned: Basic Rules --But reinterpreted --Can have unexpected effects: adding or subtrac6ng 2w --Expression containing signed and unsigned int |
(3)Expanding, truncating
p31 | Sign Extension ---Rule |
p32 | Sign Extension Example ---C automatically performs sign extension |
p33 | Summary:Expanding, Truncating: Basic Rules ---Truncating (e.g., unsigned to unsigned short) |
(4)Addition, negation, multiplication, shifting
p35 | Unsigned Addition ---Implements Modular Arithmetic |
p36 | Visualizing (Mathematical) Integer Addi6on |
p37 | Visualizing Unsigned Addition 注:overflow溢出舍弃最高位 |
p38 | Two’s Complement Addition ---TAdd and UAdd have Identical Bit-Level Behavior |
p39 | TAdd Overflow ---Functionality |
p40 | Visualizing 2’s Complement Addition ---Wraps Around |
p41 | Multiplication ---But, exact results can be bigger than w bits ---So, maintaining exact results… |
p42 | Unsigned Multiplication in C ---Implements Modular Arithmetic |
p43 | Signed Multiplication in C ---Standard Multiplication Func6on |
p44 | Power-of-2 Multiply with Shift ---Examples |
p45 | Unsigned Power-of-2 Divide with Shift --Quotient of Unsigned by Power of 2 |
(5)Summary
p47 | Arithmetic: Basic Rules --Multiplication: 溢出分析 |
p48 | Why Should I Use Unsigned? ---Don’t use without understanding implications |
p49 | Counting Down with Unsigned ---See Robert Seacord, Secure Coding in C and C++ ---Even better |
p50 | Why Should I Use Unsigned? (cont.) ---Do Use When Using Bits to Represent Sets |
4.Representations in memory, pointers, strings
p52 | Byte-Oriented Memory Organization ---Note: system provides private address spaces to each “process” 编程思想 |
p53 | Machine Words ---Any given computer has a “Word Size” |
p54 | Word-Oriented Memory Organization ---Addresses Specify Byte Locations |
p55 | Example Data Representa6ons |
p56 | Byte Ordering ---Conventions 大端(Big Endian0)、小端(Little Endian) |
p57 | Byte Ordering Example --Example |
p58 | Representing Integers |
p59 | Examining Data Representations --Code to Print Byte Representation of Data |
p60 | show_bytes Executition Example |
p61 | Representing Pointers |
p62 | Representing Strings ---Compatibility |
p63 | Integer C Puzzles 后面的为Bonus |
p65 | Application of Boolean Algebra --Applied to Digital Systems by Claude Shannon |
p66 | Binary Number Property --Assume true for w-1: |
p67 | Code Security Example ---There are legions of smart people trying to find vulnerabili6es in programs |
p68 | Typical Usage |
p69 | Malicious Usage |
p70 | Mathematical Properties |
p71 | Mathematical Properties of TAdd ---Two’s Complement Under TAdd Forms a Group |
p72 | Characterizing TAdd ---Functionality |
p73 | Negation: Complement & Increment ---Complement ---Complete Proof? |
p74 | Complement & Increment Examples |
p75 | Code Security Example #2 ---SUN XDR library |
p76 | XDR Code |
p77 | XDR Vulnerability ---How can I make this function secure? |
p78 | Compiled Multiplication Code --C compiler automa6cally generates shift/add code when multoplying by constant |
p79 | Compiled Unsigned Division Code ---For Java Users |
p80 | Signed Power-of-2 Divide with Shift ---Quo6ent of Signed by Power of 2 |
p81 | Correct Power-of-2 Divide ---Quotient of Negative Number by Power of 2 |
p82 | Correct Power-of-2 Divide (Cont.) |
p83 | Compiled Signed Division Code |
p84 | Arithmetic: Basic Rules ---Left shift ---Right shift |
p85 | Properties of Unsigned Arithmetic ---Unsigned Multiplication with Addi6on Forms Commutative Ring |
p86 | Properties of Two’s Comp. Arithmetic ---Both Form Rings ---Comparison to (Mathema6cal) Integer Arithmetic |
p87 | Reading Byte-Reversed Listings ---Example Fragment ---Deciphering Numbers |