Lecture(Ⅱ):Bits, Bytes, and Integers

目录

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

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

p22Mapping Between Signed & Unsigned
---Keep bit representations and reinterpret
p23-p24Mapping Signed ↔ Unsigned
p26Conversion 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溢出舍弃最高位

p38Two’s Complement Addition
---TAdd and UAdd have Identical Bit-Level Behavior
p39TAdd 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

p43Signed Multiplication in C
---Standard Multiplication Func6on
p44 

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

---Examples

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

(5)Summary

p47

Arithmetic: Basic Rules
--Addition:

--Multiplication:

溢出分析

p48Why 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”

编程思想

p53Machine Words
---Any given computer has a “Word Size”
p54Word-Oriented Memory Organization
---Addresses Specify Byte Locations
p55Example Data Representa6ons
p56

Byte Ordering
---So, how are the bytes within a multi-byte word ordered in memory?

---Conventions

大端(Big Endian0)、小端(Little Endian)

p57Byte Ordering Example
--Example
p58

Representing Integers

p59Examining Data Representations
--Code to Print Byte Representation of Data
p60show_bytes Executition Example
p61Representing Pointers
p62

Representing Strings
---Strings in C

---Compatibility

p63

Integer C Puzzles

后面的为Bonus

p65Application 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

p68Typical Usage
p69Malicious 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

p72Characterizing TAdd
---Functionality
p73

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

---Complement

---Complete Proof?

p74Complement & Increment Examples
p75Code Security Example #2
---SUN XDR library
p76XDR Code
p77

XDR Vulnerability
---What if:

---How can I make this function secure?

p78Compiled 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

p81Correct Power-of-2 Divide
---Quotient of Negative Number by Power of 2
p82Correct Power-of-2 Divide (Cont.)
p83Compiled Signed Division Code
p84

Arithmetic: Basic Rules
---Unsigned ints, 2’s complement ints are isomorphic rings:
isomorphism = cas6ng

---Left shift

---Right shift

p85Properties 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值