CSAPP笔记:Bits, Bytes and Integer01
course website:here
CSAPP笔记:Bits, Bytes
Representing information as bits
Everything is bits
- 0/1
- encoding/interpreting sets of bits in various ways
- instructions
- represent and manipulate numbers. sets and strings
- why use bits?
- easy to store
- reliably transmitted on noisy and inaccurate wires
Encoding Byte Values
- Binary 00000000-11111111
- Decimal 0-255
- Hexadecimal 00-FF
Example Data Representations
Bit level manipulation
Boolean algebra
Example: Representing and manipulating Sets
widely used
- symmetric difference is 对称差 in chinese:
shift
- left shift
- right shift
- logical shift
- arithmetic shift
- undefined behavior
- shift amount < 0 or >= word size
Integers
Representing signed and unsigned
Encoding Ints
Unsigned
Signed
- for two’s complement, most significant bit indicated sign
- 0 for nonnegative
- 1 for negative
Numeric Ranges
Unsigned
0 ~
Two’s Complement(universal)
Minus 1
1111…111
values for W = 16
values for different word sizes
- 64bits TMax has 18 位
Conversion, Casting
Mapping between Signed & Unsigned
- keep bit representations and reinterpret
- casting may cause some surprises , we need to be careful
- 0 == 0U
- -1 < 0
- -1 > 0U
- 2147483647U < -2147483648
what does this code return for TMin?
- it will return TMin(100…000)
why UMax = 2TMax+1?
What if i is declared unsigned?
- infinite loop: 0->UMax
another example
- infinite loop
Expanding, Truncating
Sign Extension
example:
- the above is a negative number
Truncating
- it’ so funny here, the professor does the additions wrongly for two times
- it wasn’t a very interesting example :)
- positive number may become negative number
- negative number may become positive number
- Post title:CSAPP笔记:Bits, Bytes and Integer01
- Post author:sixwalter
- Create time:2023-08-05 11:14:26
- Post link:https://coelien.github.io/2023/08/05/course-learning/CMU-213/213_bits01/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
Comments