Lecture(Ⅱ):Bits, Bytes, and Integers

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24990189/article/details/89762761

目录

1.Representing information as bits

2.Bit-level manipulations

3.Integers

(1)Representation: unsigned and signed

(2)Conversion, casting

(3)Expanding, truncating

(4)Addition, negation, multiplication, shifting

(5)Summary

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
---Representa6on

---Operations

p11

Bit-Level Operations in C
---Operations &, |, ~, ^ Available in C

---Examples (Char data type)

p12-p13

Contrast: Logic Opera6ons in C
---Contrast to Logical Operators

---Examples (char data type)  &&, ||,  !

p14

Shift Operations
---Left Shift:  x << y

---Right Shift: x >> y

---Undefined Behavior

3.Integers

(1)Representation: unsigned and signed

p16

Encoding Integers
---Unsigned、Two's Complement

注:有很多表示signed的方法,这门课我们只关注补码形式

---C short 2 bytes long

---Sign Bit

p17 Two-complement Encoding Example (Cont.)
p18

Numeric Ranges
---Unsigned Values

---Two’s Complement Values

---Other Values

p19

Values for Different Word Sizes
---Observa6ons

---C Programming

p20

Unsigned & Signed Numeric Values
---Equivalence

---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
---Constants

---Casting

p28

Casting Surprises
---Expression Evalua6on

p29

Summary:Casting Signed ↔ Unsigned: Basic Rules
--Bit paiern is maintained

--But reinterpreted

--Can have unexpected effects: adding or subtrac6ng 2w

--Expression containing signed and unsigned int

(3)Expanding, truncating

p31

Sign Extension
---Task

---Rule

p32

Sign Extension Example
---Converting from smaller to larger integer data type

---C automatically performs sign extension

p33

Summary:Expanding, Truncating: Basic Rules
---Expanding (e.g., short int to int)

---Truncating (e.g., unsigned to unsigned short)

(4)Addition, negation, multiplication, shifting

p35

Unsigned Addition
---Standard Addition Function

---Implements Modular Arithmetic

p36

Visualizing (Mathematical) Integer Addi6on
---Integer Addition

p37

Visualizing Unsigned Addition
---Wraps Around

注:overflow溢出舍弃最高位

p38 Two’s Complement Addition
---TAdd and UAdd have Identical Bit-Level Behavior
p39 TAdd Overflow
---Functionality
p40

Visualizing 2’s Complement Addition
---Values

---Wraps Around

p41

Multiplication
---Goal: Computing Product of w-bit numbers x, y

---But, exact results can be bigger than w bits

---So, maintaining exact results…

p42

Unsigned Multiplication in C
---Standard Multiplication Function

---Implements Modular Arithmetic

p43 Signed Multiplication in C
---Standard Multiplication Func6on
p44 

Power-of-2 Multiply with Shift
---Operation

---Examples

p45 Unsigned Power-of-2 Divide with Shift
--Quotient of Unsigned by Power of 2

(5)Summary

p47

Arithmetic: Basic Rules
--Addition:

--Multiplication:

溢出分析

p48 Why Should I Use Unsigned?
---Don’t use without understanding implications
p49

Counting Down with Unsigned
---Proper way to use unsigned as loop index

---See Robert Seacord, Secure Coding in C and C++

---Even better

p50

Why Should I Use Unsigned? (cont.)
---Do Use When Performing Modular Arithmetic

---Do Use When Using Bits to Represent Sets

4.Representations in memory, pointers, strings

p52

Byte-Oriented Memory Organization
---Programs refer to data by address

---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
---So, how are the bytes within a multi-byte word ordered in memory?

---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
---Strings in C

---Compatibility

p63

Integer C Puzzles

后面的为Bonus

p65 Application of Boolean Algebra
--Applied to Digital Systems by Claude Shannon
p66

Binary Number Property
--w = 0:

--Assume true for w-1:

p67

Code Security Example
---Similar to code found in FreeBSD’s implementation of getpeername

---There are legions of smart people trying to find vulnerabili6es in programs

p68 Typical Usage
p69 Malicious Usage
p70

Mathematical Properties
---Modular Addition Forms an Abelian Group

p71

Mathematical Properties of TAdd
---Isomorphic Group to unsigneds with UAdd

---Two’s Complement Under TAdd Forms a Group

p72 Characterizing TAdd
---Functionality
p73

Negation: Complement & Increment
---Claim: Following Holds for 2’s Complement

---Complement

---Complete Proof?

p74 Complement & Increment Examples
p75 Code Security Example #2
---SUN XDR library
p76 XDR Code
p77

XDR Vulnerability
---What if:

---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
---Uses logical shift for unsigned

---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
---Unsigned ints, 2’s complement ints are isomorphic rings:
isomorphism = cas6ng

---Left shift

---Right shift

p85 Properties of Unsigned Arithmetic
---Unsigned Multiplication with Addi6on Forms Commutative Ring
p86

Properties of Two’s Comp. Arithmetic
---Isomorphic Algebras

---Both Form Rings

---Comparison to (Mathema6cal) Integer Arithmetic

p87

Reading Byte-Reversed Listings
---Disassembly

---Example Fragment

---Deciphering Numbers

猜你喜欢

转载自blog.csdn.net/qq_24990189/article/details/89762761