CS 241: Systems Programming Lecture 12. Bits and Bytes 1 Fall 2019 - - PowerPoint PPT Presentation

cs 241 systems programming lecture 12 bits and bytes 1
SMART_READER_LITE
LIVE PREVIEW

CS 241: Systems Programming Lecture 12. Bits and Bytes 1 Fall 2019 - - PowerPoint PPT Presentation

CS 241: Systems Programming Lecture 12. Bits and Bytes 1 Fall 2019 Prof. Stephen Checkoway 1 Computers use binary Everything in a computer is stored and manipulated as a collection of bits The bits mean something only in how they are


slide-1
SLIDE 1

CS 241: Systems Programming Lecture 12. Bits and Bytes 1

Fall 2019

  • Prof. Stephen Checkoway

1

slide-2
SLIDE 2

Computers use binary

Everything in a computer is stored and manipulated as a collection of bits

  • The bits mean something only in how they are used, not what they are

Example with 32-bits: 01000011010100110100001101001001

  • As a integer: 1129530185
  • As a (single-precision) floating point number: 211.262833
  • As a sequence of four ASCII characters: CSCI
  • As 32-bit x86 instructions:


inc ebx
 push ebx
 inc ebx
 dec ecx

2

slide-3
SLIDE 3

Base 10 review

3

slide-4
SLIDE 4

Base 10 review

Given a decimal (base 10) number 125310

3

slide-5
SLIDE 5

Base 10 review

Given a decimal (base 10) number 125310

  • 3 ones (100)

3

slide-6
SLIDE 6

Base 10 review

Given a decimal (base 10) number 125310

  • 3 ones (100)
  • 5 tens (101)

3

slide-7
SLIDE 7

Base 10 review

Given a decimal (base 10) number 125310

  • 3 ones (100)
  • 5 tens (101)
  • 2 hundreds (102)

3

slide-8
SLIDE 8

Base 10 review

Given a decimal (base 10) number 125310

  • 3 ones (100)
  • 5 tens (101)
  • 2 hundreds (102)
  • 1 thousand (103)

103 102 101 100
 1 2 5 3

3

slide-9
SLIDE 9

Base 8 (Octal)

4

slide-10
SLIDE 10

Base 8 (Octal)

Only uses the digits 0-7

  • In C, literal starts with leading 0

4

slide-11
SLIDE 11

Base 8 (Octal)

Only uses the digits 0-7

  • In C, literal starts with leading 0

Given a octal (base 8) number 02345

  • 5 ones (80)
  • 4 eights (81)
  • 3 sixty-fours (82)
  • 2 five hundred twelves (83)

4

slide-12
SLIDE 12

Base 8 (Octal)

Only uses the digits 0-7

  • In C, literal starts with leading 0

Given a octal (base 8) number 02345

  • 5 ones (80)
  • 4 eights (81)
  • 3 sixty-fours (82)
  • 2 five hundred twelves (83)

83 82 81 80
 2 3 4 5

4

slide-13
SLIDE 13

Base 16 (Hexadecimal)

5

slide-14
SLIDE 14

Base 16 (Hexadecimal)

Single place has values of 0-15

  • Need digits larger than 9. Use A=10, B=11, …, F=15
  • In C, starts with a leading 0x or 0X

5

slide-15
SLIDE 15

Base 16 (Hexadecimal)

Single place has values of 0-15

  • Need digits larger than 9. Use A=10, B=11, …, F=15
  • In C, starts with a leading 0x or 0X

Given a hexadecimal (base 16) number 0x04E5

  • 5 ones (160)
  • 14 sixteens (161)
  • 4 two hundred fifty-sixes (162)
  • 0 four thousand ninety-sixes (163)

5

slide-16
SLIDE 16

Base 16 (Hexadecimal)

Single place has values of 0-15

  • Need digits larger than 9. Use A=10, B=11, …, F=15
  • In C, starts with a leading 0x or 0X

Given a hexadecimal (base 16) number 0x04E5

  • 5 ones (160)
  • 14 sixteens (161)
  • 4 two hundred fifty-sixes (162)
  • 0 four thousand ninety-sixes (163)

163 162 161 160

5

slide-17
SLIDE 17

Base 16 (Hexadecimal)

Single place has values of 0-15

  • Need digits larger than 9. Use A=10, B=11, …, F=15
  • In C, starts with a leading 0x or 0X

Given a hexadecimal (base 16) number 0x04E5

  • 5 ones (160)
  • 14 sixteens (161)
  • 4 two hundred fifty-sixes (162)
  • 0 four thousand ninety-sixes (163)

163 162 161 160 0 4 E 5

5

slide-18
SLIDE 18

Base 2 (Binary)

6

slide-19
SLIDE 19

Base 2 (Binary)

Only uses the digits 0 and 1

6

slide-20
SLIDE 20

Base 2 (Binary)

Only uses the digits 0 and 1 Given a binary number 0b0000010011100101

  • 1 20,

0 21, 1 22, 0 23

  • 0 24,

1 25, 1 26, 1 27

  • 0 28,

0 29, 1 210, 0 211

  • 0 212, 0 213, 0 214, 0 215

6

slide-21
SLIDE 21

Base 2 (Binary)

Only uses the digits 0 and 1 Given a binary number 0b0000010011100101

  • 1 20,

0 21, 1 22, 0 23

  • 0 24,

1 25, 1 26, 1 27

  • 0 28,

0 29, 1 210, 0 211

  • 0 212, 0 213, 0 214, 0 215

215..12 211..8 27..4 23..0

6

slide-22
SLIDE 22

Base 2 (Binary)

Only uses the digits 0 and 1 Given a binary number 0b0000010011100101

  • 1 20,

0 21, 1 22, 0 23

  • 0 24,

1 25, 1 26, 1 27

  • 0 28,

0 29, 1 210, 0 211

  • 0 212, 0 213, 0 214, 0 215

215..12 211..8 27..4 23..0 0000 0100 1110 0101

6

slide-23
SLIDE 23

Converting to decimal

7

slide-24
SLIDE 24

Converting to decimal

Multiply and sum up the digit * baseposition value

7

slide-25
SLIDE 25

Converting to decimal

Multiply and sum up the digit * baseposition value

  • 1253

= 1*103 + 2*102 + 5*101 + 3*100 = 1253

7

slide-26
SLIDE 26

Converting to decimal

Multiply and sum up the digit * baseposition value

  • 1253

= 1*103 + 2*102 + 5*101 + 3*100 = 1253

  • 02345 = 2*83 + 3*82

+ 4*81 + 5*80 = 1253

7

slide-27
SLIDE 27

Converting to decimal

Multiply and sum up the digit * baseposition value

  • 1253

= 1*103 + 2*102 + 5*101 + 3*100 = 1253

  • 02345 = 2*83 + 3*82

+ 4*81 + 5*80 = 1253

  • 0x04E5 = 0*163 + 4*162 + 14*161 + 5*160 = 1253

7

slide-28
SLIDE 28

Converting to decimal

Multiply and sum up the digit * baseposition value

  • 1253

= 1*103 + 2*102 + 5*101 + 3*100 = 1253

  • 02345 = 2*83 + 3*82

+ 4*81 + 5*80 = 1253

  • 0x04E5 = 0*163 + 4*162 + 14*161 + 5*160 = 1253
  • 0b0000010011100101 = 1253

7

slide-29
SLIDE 29

Convert the octal value 031 to decimal

  • A. 7
  • B. 25
  • C. 31
  • D. 49
  • E. 248

8

slide-30
SLIDE 30

Converting binary to hex

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-31
SLIDE 31

Converting binary to hex

Just group digits by 4s starting with LSB

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-32
SLIDE 32

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-33
SLIDE 33

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101
  • 0b 0000 0100 1110 0101

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-34
SLIDE 34

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101
  • 0b 0000 0100 1110 0101

Each block of 4 bits is 0–15

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-35
SLIDE 35

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101
  • 0b 0000 0100 1110 0101

Each block of 4 bits is 0–15

  • Replace each with a hex digit

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-36
SLIDE 36

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101
  • 0b 0000 0100 1110 0101

Each block of 4 bits is 0–15

  • Replace each with a hex digit
  • 0 4 E 5

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-37
SLIDE 37

Converting binary to hex

Just group digits by 4s starting with LSB

  • 0b0000010011100101
  • 0b 0000 0100 1110 0101

Each block of 4 bits is 0–15

  • Replace each with a hex digit
  • 0 4 E 5
  • 0x04E5

9

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-38
SLIDE 38

Converting binary to octal

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-39
SLIDE 39

Converting binary to octal

Just group digits by 3s starting with LSB

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-40
SLIDE 40

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-41
SLIDE 41

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101
  • 0b 000 000 010 011 100 101

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-42
SLIDE 42

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101
  • 0b 000 000 010 011 100 101

Each block of 3 bits is 0–7

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-43
SLIDE 43

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101
  • 0b 000 000 010 011 100 101

Each block of 3 bits is 0–7

  • Replace each with a octal digit

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-44
SLIDE 44

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101
  • 0b 000 000 010 011 100 101

Each block of 3 bits is 0–7

  • Replace each with a octal digit
  • 0 0 2 3 4 5

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-45
SLIDE 45

Converting binary to octal

Just group digits by 3s starting with LSB

  • 0b0000010011100101
  • 0b 000 000 010 011 100 101

Each block of 3 bits is 0–7

  • Replace each with a octal digit
  • 0 0 2 3 4 5
  • 0002345 (We prepended a 0 to denote octal)

10

Octal Binary 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

slide-46
SLIDE 46

Convert the 16-bit binary number 0b11001010_11111110 to hex. (I added an underscore to separate the two groups of 8 bits to improve readability.)

  • A. 0xBEEF
  • B. 0xCAFE
  • C. 0xDEAD
  • D. 0xFACE
  • E. 0xFEED

11

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-47
SLIDE 47

Alternative view of hex/octal

Binary is a pain to read/work with

  • Consider a 64-bit number


0b010011000100000011010110101100000011011011000101010 0011110110000
 so long it doesn't fit on one line! Hex (and much less commonly octal) can be viewed as a much more compact way to represent binary numbers

  • 0x4c40d6b036c547b0

12

slide-48
SLIDE 48

Converting hex/octal to binary

  • 1. Take each hexadecimal (or octal) digit
  • 2. Convert it into binary
  • 4 places hex (e.g., A becomes 1010)
  • 3 places octal (e.g., 6 becomes 110)
  • 3. Group them together from LSB to MSB

13

slide-49
SLIDE 49

Converting between Hex & Octal

  • 1. Take hexadecimal number
  • 2. Convert to binary
  • 3. Regroup in clusters of 3 from LSB
  • 4. Generate Octal digits
  • 5. Use reverse process for Octal to Hex

14

slide-50
SLIDE 50

Converting decimal to binary

15

slide-51
SLIDE 51

Converting decimal to binary

Repeatedly divide by 2, recording remainders

15

slide-52
SLIDE 52

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant

15

slide-53
SLIDE 53

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

15

slide-54
SLIDE 54

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1

15

slide-55
SLIDE 55

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1

15

slide-56
SLIDE 56

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1
  • 9 / 2 = 4 r 1

15

slide-57
SLIDE 57

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1
  • 9 / 2 = 4 r 1
  • 4 / 2 = 2 r 0

15

slide-58
SLIDE 58

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1
  • 9 / 2 = 4 r 1
  • 4 / 2 = 2 r 0
  • 2 / 2 = 1 r 0

15

slide-59
SLIDE 59

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1
  • 9 / 2 = 4 r 1
  • 4 / 2 = 2 r 0
  • 2 / 2 = 1 r 0
  • 1 / 2 = 0 r 1

15

slide-60
SLIDE 60

Converting decimal to binary

Repeatedly divide by 2, recording remainders Remainders form the binary number from least to most significant Example: 39

  • 39 / 2 = 19 r 1
  • 19 / 2 = 9 r 1
  • 9 / 2 = 4 r 1
  • 4 / 2 = 2 r 0
  • 2 / 2 = 1 r 0
  • 1 / 2 = 0 r 1
  • 39 = 0b100111

15

slide-61
SLIDE 61

In-class exercise

https://checkoway.net/teaching/cs241/2019-fall/exercises/Lecture-12.html Grab a laptop and a partner and try to get as much of that done as you can!

16