CSAPP笔记:Bits, Bytes and Integer01
sixwalter Lv6

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
image-20230406101623090

Encoding Byte Values

  • Binary 00000000-11111111
  • Decimal 0-255
  • Hexadecimal 00-FF

Example Data Representations

image-20230406102532287

Bit level manipulation

Boolean algebra

image-20230406102846832

Example: Representing and manipulating Sets

widely used

image-20230406103402468
  • symmetric difference is 对称差 in chinese:
image-20230406103458899

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

image-20230406105453744

Signed

  • for two’s complement, most significant bit indicated sign
    • 0 for nonnegative
    • 1 for negative
image-20230406105401555

Numeric Ranges

Unsigned

0 ~

Two’s Complement(universal)

~

Minus 1

1111…111

values for W = 16

image-20230406110240459

values for different word sizes

image-20230406110440140
  • 64bits TMax has 18 位

Conversion, Casting

Mapping between Signed & Unsigned

image-20230406111407535
  • keep bit representations and reinterpret
image-20230406111610856 image-20230406111946568 image-20230406112126993
  • 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?

image-20230406113449103
  • it will return TMin(100…000)

why UMax = 2TMax+1?

image-20230406123934287

What if i is declared unsigned?

image-20230406124159243
  • infinite loop: 0->UMax

another example

image-20230406124603747
  • infinite loop

Expanding, Truncating

Sign Extension

image-20230406125037825

example:

image-20230406125553803 image-20230406125315446 image-20230406125444452
  • the above is a negative number

Truncating

image-20230406125856661
  • it’ so funny here, the professor does the additions wrongly for two times
image-20230406130123284
  • it wasn’t a very interesting example :)
image-20230406130241157
  • 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