Introduction to Programming -...

31
Introduction to Programming session 4 Spring 2011 Instructor: Reza Entezari-Maleki Email: [email protected] Sharif University of Technology 1

Transcript of Introduction to Programming -...

Introduction to Programmingsession 4

Spring 2011

Instructor: Reza Entezari-Maleki

Email: [email protected]

Sharif University of Technology 1

Outlines

� Binary numbers

� Addition

� Subtraction

� Division� Division

� Multiplication

� One’s and Two’s Complement

� Bitwise Operators

2

Binary numbers

� Binary numbers� Why binary?

� Computers are built using digital circuits� Inputs and outputs can have only two values� True (high voltage) or false (low voltage)

� Converting base 10 to base 2Converting base 10 to base 2� Octal and hexadecimal

� Integers� Unsigned integers� Integer addition� Signed integers

� C bit operators� And, or, not, and xor� Shift-left and shift-right

3

Base 10 and Base 2

� Base 10� Each digit represents a power of 10

� 4173 = 4*103 + 1*102 + 7*101 + 3*100

� Base 2Each bit represents a power of 2� Each bit represents a power of 2

� 10110 = 1*24 + 0*23 + 1*22 + 1*21 + 0*20 = 22

� Question:� What is the binary representation of number12 ?

� Response:� 1100

4

Base 8

� Octal (base 8)� Digits 0, 1, …, 7

� Thus the 12 bit binary 110 010 101 001number converted to Oct is:

000 = 0

001 = 1

010 = 2

011 = 3

100 = 4

101 = 5

110 = 6number converted to Oct is:

6251

5

110 = 6

111 = 7

Base 8

� Question:� What is the octal representation of number 118 (in base 10) ?

� Response:� 166

� Question:� What is the octal representation of number 1011000101 (in base 2) ?� What is the octal representation of number 1011000101 (in base 2) ?

� Response:� 1305

� Question:� What is the binary representation of number 1472 (in base 8) ?

� Response:� 1100111010

6

Base 16

� Hexadecimal (base 16)� Digits 0, 1, …, 9, A, B, C, D, E, F

� Thus the 16-bit binary number 1011 0010 1010 1001converted to Hex is: B2A9

7

0000 = 0 1000 = 8

0001 = 1 1001 = 9

0010 = 2 1010 = A

0011 = 3 1011 = B

0100 = 4 1100 = C

0101 = 5 1101 = D

0110 = 6 1110 = E

0111 = 7 1111 = F

Base 16

� Question:� What is the hexadecimal representation of number 375 (in base 10) ?

� Response:� 177

� Question:� What is the hexadecimal representation of number 1011000101(in base 2) ?� What is the hexadecimal representation of number 1011000101(in base 2) ?

� Response:� 2C5

� Question:� What is the binary representation of number 6A4D2 (in base 16) ?

� Response:� 01101010010011010010

8

� Fixed number of bits in memory� Short: usually 16 bits� Int: 16 or 32 bits� Long: 32 bits

� Unsigned integer� No sign bit

Integers

� No sign bit� Always positive or 0

� Example of unsigned int� 00000001 � 1� 00001111 � 15� 00010000 � 16� 00100001 � 33� 11111111 � 255

9

Decimal Addition

1) Add 8 + 7 = 15Write down 5, carry 1

11 12) Add 5 + 5 + 1 = 11

Add 3758 to 4657:

10

3 7 5 8

+ 4 6 5 7

1

8

1 1

4 1 5 4) Add 3 + 4 + 1 = 8Write down 8

3) Add 7 + 6 + 1 = 14Write down 4, carry 1

2) Add 5 + 5 + 1 = 11Write down 1, carry 1

Binary Addition

Rules:� 0 + 0 = 0

� 0 + 1 = 1� 0 + 1 = 1

� 1 + 0 = 1 (just like in decimal)

� 1 + 1 = 210= 102 = 0 with 1 to carry

� 1 + 1 + 1 = 310= 112 = 1 with 1 to carry

Binary Addition …

Example 1: Add

binary 110111 to 11100

Col 1) Add 1 + 0 = 1

Write 1

Col 2) Add 1 + 0 = 1

Write 1

Col 3) Add 1 + 1 = 2 (10 in binary)

1 1 0 1 1 1

+ 0 1 1 1 0 0

1

1

111

0 1 0 0 11

Col 3) Add 1 + 1 = 2 (10 in binary)

Write 0, carry 1

Col 4) Add 1+ 0 + 1 = 2

Write 0, carry 1

Col 6) Add 1 + 1 + 0 = 2

Write 0, carry 1

Col 5) Add 1 + 1 + 1 = 3 (11 in binary)

Write 1, carry 1

Col 7) Bring down the carried 1

Write 1

Binary Addition …

Verification1101112 � 5510

+0111002 + 2810

8310

You can always check youranswer by converting thefigures to decimal, doing theaddition, and comparing the 8310

64 32 16 8 4 2 1

1 0 1 0 0 1 1

= 64 + 16 + 2 +1

= 8310

1 1 0 1 1 1

+ 0 1 1 1 0 0

1 0 1 0 0 11

addition, and comparing theanswers.

Decimal Subtraction

Subtract 4657 from 8025:

7 9 11

1) Try to subtract 5 – 7 � can’t.

Must borrow 10 from next column.

2) Try to subtract 1 – 5 � can’t.

Add the borrowed 10 to the original 5.

Then subtract 15 – 7 = 8.

8 0 2 5

- 4 6 5 7

7 9 11

11

8633

4) Subtract 7 – 4 = 3

3) Subtract 9 – 6 = 3

2) Try to subtract 1 – 5 � can’t.

Must borrow 10 from next column.

But next column is 0, so must go to

column after next to borrow.

Add the borrowed 10 to the original 0.

Now you can borrow 10 from this column.

Add the borrowed 10 to the original 1..

Then subtract 11 – 5 = 6

Decimal Subtraction …

8633

8 0 2 5

- 4 6 5 7

�So when you cannot subtract, you borrow from the column to the left.

�The amount borrowed is 1 base unit, which in decimal is 10.

�The 10 is added to the original column value, so you will be able to subtract.

Binary Subtraction

• In binary, the base unit is 2

• So when you cannot subtract, you borrow from the column to the left.

• The amount borrowed is 2. • The amount borrowed is 2.

• The 2 is added to the original column value, so you will be able to subtract.

Binary Subtraction …

Example 1: Subtractbinary 11100 from 110011

Col 1) Subtract 1 – 0 = 1

Col 3) Try to subtract 0 – 1 � can’t.

Must borrow 2 from next column.

But next column is 0, so must go to

column after next to borrow.

Col 2) Subtract 1 – 0 = 1

1 1 0 0 1 1

- 1 1 1 0 0

2

0 0 2

1

2

1101 Col 5) Try to subtract 0 – 1 � can’t.

Must borrow from next column.

Col 4) Subtract 1 – 1 = 0

column after next to borrow.

Add the borrowed 2 to the 0 on the right.

Now you can borrow from this column

(leaving 1 remaining).

Add the borrowed 2 to the original 0.

Then subtract 2 – 1 = 1

1

Add the borrowed 2 to the remaining 0.

Then subtract 2 – 1 = 1Col 6) Remaining leading 0 can be ignored.

Binary Subtraction …

Verification

1100112 � 5110

- 111002 - 2810

23102 1

Subtract binary

11100 from 110011:

64 32 16 8 4 2 1

1 0 1 1 1

= 16 + 4 + 2 + 1

= 2310

1 1 0 0 1 1

- 1 1 1 0 0

2

0 0 2 2

1101 1

Binary Subtraction …

Example 2: Subtract

binary 10100 from 101001

Verification

1010012 � 4110

- 101002 - 2010

2110

1 0 1 0 0 1

- 1 0 1 0 0

20 0 2

1101 0

64 32 16 8 4 2 1

1 0 1 0 1

= 16 + 4 + 1

= 2110

� Consider only numbers in a range� E.g., five-digit car odometer: 0, 1, …, 99999

� E.g., eight-bit numbers 0, 1, …, 255

� Roll-over when you run out of space

One’s and Two’s Complement

� Roll-over when you run out of space� E.g., car odometer goes from 99999 to 0, 1, …

� E.g., eight-bit number goes from 255 to 0, 1, …

20

One’s and Two’s Complement …

� One’s complement: flip every bit� E.g., b is 01000101 (i.e., 69 in base 10)

� One’s complement is 10111010

� That’s simply 255-69

� Subtracting from 11111111 is easy (no carry needed!)

21

� Subtracting from 11111111 is easy (no carry needed!)

� Two’s complement� Add 1 to the one’s complement

� E.g., (255 – 69) + 1 � 1011 1011

- 0100 0101

1111 1111

1011 1010

b

one’s complement

� Computing “a – b” for unsigned integers� Same as “a + 256 – b”� Same as “a + (255 – b) + 1”� Same as “a + onecomplement(b) + 1”� Same as “a + twocomplement(b)”

One’s and Two’s Complement …

� Same as “a + twocomplement(b)”

� Example: 172 – 69� The original number 69: 0100 0101� One’s complement of 69: 1011 1010 � Two’s complement of 69: 1011 1011� Add to the number 172: 1010 1100� The sum comes to: 0110 0111� Equals: 103 in base 10

22

1010 1100

+ 1011 1011

1 0110 0111

� Sign-magnitude representation� Use one bit to store the sign

� Zero for positive number� One for negative number

� Examples� E.g., 0010 1100 � 44

One’s and Two’s Complement …

E.g., 0010 1100 44� E.g., 1010 1100 � -44

� Hard to do arithmetic this way, so it is rarely used� Complement representation

� One’s complement� Flip every bit� E.g., 1101 0011 � -44

� Two’s complement� Flip every bit, then add 1� E.g., 1101 0100 � -44

23

Bitwise Operators

� Bitwise AND (&) � Bitwise OR (|)

&

0

1

0 1

0 0

0 1

|

0

1

0 1

0 1

1 11 0 1 1 1 1

� Bitwise XOR (^)

^

0

1

0 1

0 1

1 0

� Bitwise NOT (~)

~

0 1

1 0

Bitwise Operators …

� Shift left (<<): Multiply by powers of 2� Shift some # of bits to the left, filling the blanks with 0

0 0 1 1 0 1 0 052

1 1 0 1 0 0 0 052<<2

25

� Shift right (>>): Divide by powers of 2� Shift some # of bits to the right

� For unsigned integer, fill in blanks with 0

�Question: What about signed integers?

0 0 1 1 0 1 0 052

0 0 0 0 1 1 0 152>>2

� SHR # bits means to shift right # times

Ex 1: SHR 3 1 0 0 1 1 1 0 0

0 0 0 1 0 0 1 1 1 0 0 = 0 0 0 1 0 0 1 1

Bitwise Operators …

0 0 0 1 0 0 1 1 1 0 0 = 0 0 0 1 0 0 1 1

Ex 2: SHR 2 1 1 1 0 0 1 1 0

= 0 0 1 1 1 0 0 1

26

Bitwise Operators …

ROL

ROL (rotate left) shifts each bit one place to the left. The originalleftmost bit is shifted into the rightmost position. No bits are lost.

Ex 1. ROL 1 1 0 1

Ex 2. ROL 1 1 0 0

= 1 0 0 1

1 0 1 1

Bitwise Operators …

� ROL # bits means to rotate left # times

Ex 1: ROL 3 1 0 0 1 1 1 0 0

28

Ex 1: ROL 3 1 0 0 1 1 1 0 0

= 1 1 1 0 0 1 0 0

Ex 2: ROL 2 1 1 1 0 0 1 1 0

= 1 0 0 1 1 0 1 1

Bitwise Operators …

ROR

ROR (rotate right) shifts each bit one place to the right. Theoriginal rightmost bit is shifted into the leftmost position. Nobits are lost.bits are lost.

Ex 1. ROR 1 0 1 1

Ex 2. ROR 0 0 1 1

= 1 0 0 1

1 0 11

� Multiplication in the binary system works the same way as in the decimal system: � 1*1=1 � 1*0=0 � 0*1=0

Binary Multiplication

� 0*1=0

� Example: 101

* 11______ 101 1010

______ 1111

30

� Follow the same rules as in decimal division.

� For Example: 111011/11

� Example: 111011 1111 10011

Binary Division

11 10011______ 0101 11

______ 010 1

11____010

31