cs103 guest lecturebits.usc.edu/files/cs103/coursework/cs103lecture_bin... · 2016. 8. 16. · •...

42
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations

Upload: others

Post on 11-Nov-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

1

CS103 Guest Lecture

Number Systems & ConversionBitwise Logic Operations

Page 2: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

2

Why 1’s and 0’s• Transistors are electronic devices used to build computer hardware

– Like a switch (2 positions)– Conducting / Non‐conducting– Output voltage of a transistor will either be high or low

• 1’s and 0’s are arbitrary symbols representing high and low voltage outputs.

• 2 states of the transistor lead to only 2 values in computer hardware

Low Voltage0V

-12V

High Voltage+5V

+12V1

0or

on

offControlling

Input(Gate )

Output (Drain )

Source

The voltage here determines if current

can flow between drain and source

Schematic Symbol of a Transistor

Functional View of a Transistor as a Switch

circuit is open (off) –no current can flow

circuit is closed (on) –current can flow

Circuit Diagram of a Switch Circuit Diagram of a Switch

---

-

- -- -

Page 3: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

3

POSITIONAL NUMBER SYSTEMS

Page 4: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

4

Interpreting Binary Strings

• Given a string of 1’s and 0’s, you need to know the representation system being used, before you can understand the value of those 1’s and 0’s.

• Information (value) = Bits + Context (System)

01000001 = ?

6510 ‘A’ASCII

Unsigned Binary system ASCII

system

Signed System

Page 5: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

5

Binary Number System

• Humans use the decimal number system– Based on number 10– 10 digits: [0‐9]

• Because computer hardware uses digital signals with 2 states, computers use the binary number system– Based on number 2– 2 binary digits (a.k.a bits): [0,1]

Page 6: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

6

Number Systems• Number systems consist of 

1. A base (radix) r2. r coefficients [0 to r‐1]

• Human System:  Decimal (Base 10): 0,1,2,3,4,5,6,7,8,9

• Computer System: Binary (Base 2): 0,1• Human systems for working with computer systems (shorthand for human to read/write binary)– Octal (Base 8): 0,1,2,3,4,5,6,7– Hexadecimal (Base 16): 0‐9,A,B,C,D,E,F (A thru F = 10 thru 15)

Page 7: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

7

Anatomy of a Decimal Number• A number consists of a string of explicit coefficients (digits).• Each coefficient has an implicit place value which is a power 

of the base.• The value of a decimal number (a string of decimal 

coefficients) is the sum of each coefficient times it place valueradix

(base)

(934)10 =

(3.52)10 =

Page 8: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

8

Anatomy of a Decimal Number• A number consists of a string of explicit coefficients (digits).• Each coefficient has an implicit place value which is a power 

of the base.• The value of a decimal number (a string of decimal 

coefficients) is the sum of each coefficient times it place value

Explicit coefficientsImplicit place values

radix (base)

(934)10 = 9*102 + 3*101 + 4*100 = 934

(3.52)10 = 3*100 + 5*10‐1 + 2*10‐2 = 3.52

Page 9: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

9

Positional Number Systems (Unsigned)

• A number in base r has place values/weights that are the powers of the base

• Denote the coefficients as: ai

r -1 r -2r 1 r 0

.r 2r 3

Left-most digit = Most Significant

Digit (MSD)

Right-most digit = Least Significant

Digit (LSD)

...... a -1 a -2a 1 a 0a 2a 3

Nr = Σi(ai*ri) = D10

Page 10: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

10

Examples

(746)8 =

(1A5)16 =

Page 11: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

11

Examples

(746)8 = 7*82 + 4*81 + 6*80= 448 + 32 + 16 = 48610

(1A5)16 = 1*162 + 10*161 + 5*160= 256 + 160 + 5 = 42110

Page 12: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

12

Anatomy of a Binary Number

• Same as decimal but now the coefficients are 1 and 0 and the place values are the powers of 2

(1011)2 = 1*23 + 0*22 + 1*21 + 1*20

Least Significant Bit (LSB)

Most Significant Digit (MSB)

coefficientsplace values= powers of 2

radix (base)

Page 13: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

13

Binary Examples

(1001.1)2 =

(10110001)2 =

Page 14: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

14

Binary Examples

(1001.1)2 = 8 + 1 + 0.5 = 9.510.51248

(10110001)2 = 128 + 32 + 16 + 1 = 177101632128 1

Page 15: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

15

Powers of 220 = 121 = 222 = 423 = 824 = 1625 = 3226 = 6427 = 12828 = 25629 = 512210 = 1024

512 256 128 64 32 16 8 4 2 11024

Page 16: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

16

Practice On Your Own

(11010)2 = 1*24 + 1*23 + 1*21

= 16 + 8 + 2 = (26)10

(6523)8 = 6*83 + 5*82 + 2*81 + 3*80= 3072 + 320 + 16 + 3 = (3411)10

(AD2)16 = 10*162 + 13*161 + 2*160

= 2560 +  208 + 2 = (2770)10

• Decimal equivalent is…… the sum of each coefficient multiplied by its

implicit place value (power of the base)

= Σi(ai * ri) [ai = coefficient, r = base]

Page 17: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

17

Shifting in Binary

• Move bits to the left or right with 0's shoved in one side and dropping bits on the other

• Useful for multiplying and dividing by power of 2.– Right shift by n‐bits = Dividing by 2n

– Left shift by n‐bits = Multiplying by 2n

0 0 ... 0 0 1 1

Right Shift by 2 bits:

... 0 1 1 0 0 0 0 0

Left Shift by 3 bits:

0’s shifted in… 0’s shifted in…

0 ... 0 1 1 0 0 = +12

= +3 = +96

Page 18: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

18

Bottom‐Up Conversion & Shifting

• X = 0112 = 

• 01102

• 011012

• 0110102

Page 19: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

19

Unique Combinations

• Given n digits of base r, how many unique numbers can be formed?  rn

– What is the range?  [0 to rn‐1]

Main Point: Given n digits of base r, rn unique numbers can be made with the range [0 - (rn-1)]

2-digit, decimal numbers (r=10, n=2)

3-digit, decimal numbers (r=10, n=3)

4-bit, binary numbers (r=2, n=4)

6-bit, binary numbers (r=2, n=6)

0-90-9

100 combinations:00-99

0-10-10-10-1

1000 combinations:000-999

16 combinations:0000-1111

64 combinations:000000-111111

Page 20: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

20

CONVERSION FROM DECIMAL TO OTHER BASE

Page 21: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

21

Conversion: Base 10 to Base r

• X10 = (?)r• General Method (base 10 to arbitrary base r)

– Division Method for integer portion or number 

• Alternate Method– Left‐to‐right (greedy) approach

(45)10= (?)r

Page 22: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

22

Division Method Explanation

24 23 22 21 20

a4 a3 a2 a1 a04510=

4510= a424 + a323 + a222 + a121 + a020

0.

4510= a424 + a323 + a222 + a121 + a020

2 2

22.510= a423 + a322 + a221 + a120 + a02-1

Page 23: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

23

Binary Division Method Example

4510 = (??)2

Page 24: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

24

Binary Division Method Example

4510 = (??)2

4522

22

11252

0Keep dividing until you reach 0

rem. = 1rem. = 0rem. = 1rem. = 1

MSB

LSB

4510 = (101101)2

Remainders form the number in

base r (order from bottom up)

2212 rem. = 0

rem. = 1

Page 25: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

25

Division Method• Converts integer portion of a decimal number to base r

• Informal Algorithm– Repeatedly divide number by r until equal to 0

– Remainders form coefficients of the number base r

– Remainder from last division = MSD (most significant digit) 

19310 = (??)5

19338

55

75150

Keep dividing until you reach 0

rem. = 3rem. = 3rem. = 2rem. = 1

MSD

LSD

19310 = (1233)5

Remainders form the number in

base r (order from bottom up)

Page 26: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

26

How number conversion works

• Each time we divide by r, another coefficient “falls out” and all the other place values are reduced by a factor of r.

4510 = a4 a3 a2 a1 a0

24 23 22 21 20

4510 = a424 + a323 + a222 + a121 + a020

4510 = a424 + a323 + a222 + a121 + a020 = a423 + a322 + a221 + a120 + a0

2 2 2

Quotient Rem.

a423 + a322 + a221 + a120 = a422 + a321 + a220 + a1

2 2

Quotient Rem.

More bits may be required for this actual example, but we'll use 5 to illustrate…

This is just explanation for what you've learned…Focus on the conversion process outlined earlier

Page 27: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

27

How number conversion works

• Each time we divide by r, another coefficient “falls out” and all the other place values are reduced by a factor of r.

raarara

rr

rararara

rD n

n

nn

nn 0

11

22

10

11

22

1110 )(

a0 is the remainder

D10 written as coefficients * place values

Factor r out of numerator terms with an-1 – a1

This is just explanation for what you've learned…Focus on the conversion process outlined earlier

Page 28: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

28

Left‐To‐Right Method

• An alternative to the division method• To convert a decimal number, x, to binary:

– Only coefficients of 1 or 0.  So simply find place values that add up to the desired values, starting with larger place values and proceeding to smaller values and place a 1 in those place values and 0 in all others

16 8 4 2 12510 = 1 1 1

32

For 2510 the place value 32 is too large to include so we include 16. Including 16 means we have to make 9 left over. Include 8

and 1.

0 00

Page 29: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

29

Left‐To‐Right Binary Examples

7310=128 64 32 16 8 4 2 1

.5 .25 .125 .0625 .03125

0    1    0     0    1     0    0    1

0    1    0     1    0     1    1    1

1     0   0     1    0    0     0     1

1    0     1      0     0

8710=

14510=

0.62510=

Page 30: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

30

Left‐To‐Right In Other Bases

• Can use the left‐to‐right method to convert a decimal number, x, to any base r:– Use the place values of base r (powers of r).  Starting 

with largest place values, fill in coefficients that sum up to desired decimal value without going over.

16 1

7510 = 4 B256

0 hex

Page 31: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

31

SHORTHAND FOR BINARYHexadecimal and Octal

Page 32: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

32

Binary, Octal, and Hexadecimal

• Octal (base 8 = 23)• 1 Octal digit ( _ )8 can represent: 0 – 7 

• 3 bits of binary (_ _ _)2can represent: 000‐111 = 0 – 7 

• Conclusion…1 Octal digit = 3 bits

• Hex (base 16=24)• 1 Hex digit ( _ )16 can represent: 0‐F (0‐15)

• 4 bits of binary (_ _ _ _)2 can represent: 0000‐1111= 0‐15

• Conclusion…1 Hex digit = 4 bits 

Page 33: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

33

Binary to Octal or Hex

• Make groups of 3 bits starting from radix point and working outward

• Add 0’s where necessary• Convert each group of 3 to an octal digit

101001110.11101001110.11 0 00 000

• Make groups of 4 bits starting from radix point and working outward

• Add 0’s where necessary

• Convert each group of 4 to an octal digit

516.68 14E.C16

5 1 6 6 1 4 E C

Page 34: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

34

Octal or Hex to Binary

• Expand each octal digit to a group of 3 bits

• Expand each hex digit to a group of 4 bits

317.28 D93.816

011001111.0102 110110010011.10002

11001111.012 110110010011.12

Page 35: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

35

LOGIC OPERATIONS

Page 36: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

36

Bitwise Logical Operations

B1 B2 F0 0 0

0 1 0

1 0 0

1 1 1

ANDB1

B2

Pas

sFo

rce

'0'

ZX

YXOR

B1

B2F

B1 B2 F0 0 0

0 1 1

1 0 1

1 1 0

Pas

sIn

vert

B1 B2 F0 0 0

0 1 1

1 0 1

1 1 1

Pas

sFo

rce

'1'

B1

B2OR

0 | x = x1 | x = 1x | x = x

0 & x = 01 & x = xx & x = x

0 ^ x = x1 ^ x = ~ xx ^ x = 0

X F1 0

0 1

F = X or ~X

Page 37: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

37

Logical Operations• Logic operations on numbers means performing the operation on each pair of bits

0xF0AND 0x3C

0x30

1111 0000AND 0011 1100

0011 0000

0xF0OR 0x3C

0xFC

1111 0000OR 0011 1100

1111 1100

0xF0XOR 0x3C

0xCC

1111 0000XOR 0011 1100

1100 1100

NOT 0xAC0x53

NOT 1010 11000101 0011

Page 38: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

38

Logical Operations• The C language has two types of logic operations

– Logical  and Bitwise• Logical Operators (&&, ||, !) 

– Operate on the logical value of a FULL variable (char, int, etc.) interpreting that value as either True (non‐zero) or False (zero)char x = 1,  y = 2, z;z = x && y;

– Result is z = 1;   Why?• Bitwise Logical Operators (&, |, ^, ~)

– Operate on the logical value of INDIVIDUAL bits in a variablechar x = 1,  y = 2, z;z = x & y;

– Result is z = 0;  Why?

Page 39: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

39

Logical Operations• Logic operations on numbers means performing the operation on each pair of bits

0x7AAND 0xEC

0x30

0x36OR 0x91

0xFC

0x3C0x78

XOR 0x3C0x78

Page 40: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

40

Look Toward LFSR PA

• One of your next PA's will utilize the bitwise XOR operator and leverage the fact that:– a ^ b ^ a  =  b

• Proof:

Page 41: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

41

Logical Operations• Bitwise logic operations are often used for "bit fiddling"– Change the value of a bit in a register w/o affecting other bits

– C operators: & = AND, | = OR, ^ = XOR, ~ = NOT

• Examples (Assume an 8‐bit variable, v)– Clear the LSB to '0' w/o affecting other bits

• v = v & 0xfe;– Set the MSB to '1' w/o affecting other bits

• v = v | 0x80;– Flip the LS 4‐bits w/o affecting other bits

• v = v ^ 0x0f;

?v ? ? ? ? ? ? ?

7

& _________________

?v ? ? ? ? ? ? 0

?v ? ? ? ? ? ? ?

| _________________

?v ? ? ? ? ? ?1

?v ? ? ? ? ? ? ?

^ 0 0 0 0 1 1 1 1

?v ? ? ? ? ? ??

Bit #

6 5 4 3 2 1 0

Page 42: CS103 Guest Lecturebits.usc.edu/files/cs103/coursework/CS103Lecture_Bin... · 2016. 8. 16. · • Convert each group of 3 to an octal digit 101001110.11 0 0000101001110.11 0 •

42

Exercises for Practice

• Q1‐Q15 on the posted worksheet – http://bits.usc.edu/files/cs103/coursework/NumberSys.pdf