CSAPP笔记:Bits, Bytes and Integer02
sixwalter Lv6

CSAPP笔记:Bits, Bytes and Integer02

Integers

Addition

Unsigned Addition

w is the word size(bit)

image-20230412092651606 image-20230412093232344 image-20230412093601189
  • substract 2^w from the sum

Two’s Complement Addition

image-20230412093935835

negative overflow

image-20230412094244452

positive overflow

image-20230412094403665 image-20230412094556972

Multiplication

unsigned

signed

image-20230412095629642

Divide with Shift

  • round down to zero
image-20230412100858024

Negative(~ + 1)

image-20230412101131607

When should use Unsigned?

  • when performing modualr arithmetic
  • using bits to represent sets

Representations in memory, pointers and strings

Byte-Oriented Memory Organization

image-20230412103234468
  • An address is like an index into that array

  • system provides private address spaces to each “process”

  • 32 bit machines Limits addresses to 4GB

gcc -m {32,64}

Word-Oriented Memory Organization

group bytes to word

image-20230412104558655
  • assume the address of the word is the loweset address of byte in it

Example Data Representation

image-20230412104845738

Byte Ordering

how are the bytes within a word ordered in memory?

  • Big Endian: internet, Sun

    least significant byte has highest address

  • little Endian: x86, Arm, IOS, Windows

    least significant byte has lowest address

an example

  • Variable x has 4-byte value of 0x01234567
  • Address given by &x is 0x100

image-20230412111130674

another example

  • 15213
  • binary: 0011 1011 0110 1101
  • hex: 3 B 6 D
image-20230412111626710

a useful code to print Byte Representation of Data

1
2
3
4
5
6
typedef unsigned char * pointer;
void show_bytes(pointer start, size_t len){
size_t i;
for(i = 0; i<len; ++i)
printf("%p\t0x%.2x\n",start+i, start[i]);
}
image-20230412112510479

Representing strings

  • array of characters
  • ASCII format
  • null-terminated
image-20230412130647124

Integer C Puzzles

  • x<0 -> ((x*2)<0)

    False Tmin: “100000000” -> “00000000”

  • ux >= 0

    True

  • x&7 == 7 -> (x<<30) <0

    True “1100…000”

  • ux > -1

    never true False

  • x>y -> -x<-y

    False Tmin: -Tmin is still Tmin

  • x*x >=0

    False

  • x>0 && y>0 -> x+y > 0

    False

  • x >=0 -> -x<=0

    True (Tmax)

  • x <=0 -> -x>=0

    False(Tmin)

  • (x|-x)>>31 == -1

    False(0)

  • Post title:CSAPP笔记:Bits, Bytes and Integer02
  • 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_bits02/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments