21
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS [email protected]

Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS [email protected]

Embed Size (px)

Citation preview

Page 1: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Intro to CS – Honors IRepresenting NumbersGEORGIOS PORTOKALIDIS

[email protected]

Page 2: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Today’s Lecture Numerical systems

How do computers represent numbers

Operations on integers

Signed and unsigned numbers

Page 3: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Numerical Systems We all know well the decimal system

Base 10 system

The number can be actually expressed as: 4x102 + 6x101 + 6x100

So a number N base 10 is denoted as (N)10◦ uses digits 0..9◦ And a digit d in position i has the value dix10i

How about base 2, (N)2?

Can you also do base 16 or 8?

4 6 6

OnesTens

Hundreds

102101 100

29 28 27 26 25 24 23 22 21 20

512

256

128

64 32 16 8 4 2 1

Page 4: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Decimal to Binary A simple algorithm Let D= the number we wish to convert from decimal to binary

Find P, such that 2P is the largest power of two smaller or equal to D.

Repeat until P<0 ◦ If 2P<=D then

◦ Put 1 into column P ◦ Subtract 2P from D

◦ Else ◦ Put 0 into column P

◦ Subtract 1 from P

(356)10 (??)2

Position 9 8 7 6 5 4 3 2 1 02P 29 28 27 26 25 24 23 22 21 20

Value 512 256 128 64 32 16 8 4 2 1

Page 5: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Decimal to Binary (cont’d) All binary numbers N can be represented as

B = dix2i + di-1x2i-1+...+d1x21 + d0x20 ,

where dibinary digit in position i

Odd numbers have d0=1 and even d0=0◦ This reveals the rightmost bit of N

We need to shift the number to the right by 1 bit to again calculate its last bit

◦ (B - d0x20)/2 dix2i-1 + di-1x2i-2+...+d1x20

An alternative algorithm Let D= the number we wish to convert from decimal to binary

Repeat until D equals 0◦ Divide D with 2 (D/2)◦ Prepend the remainder of the division to the left

of the binary number◦ Let D be the quotient of the division

(257)10 (??)2

Page 6: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Hexadecimal Base 16 number system

Digits are [0..9] [A..F]◦ Letters are case insensitive

Converting from binary to hexadecimal◦ There are some benefits 16 being a power of 2◦ What is 1010 0010 in hex?◦ Answer: 0xC2

◦ Note the ‘0x’ prefix!

Converting from decimal to hex◦ Use the same algorithm as for converting decimal to binary◦ (76)10 (??)16

Hexadecimal Binary Decimal

0 0000 0

1 0001 1

2 0010 2

3 0011 3

4 0100 4

5 0101 5

6 0110 6

7 0111 7

8 1000 8

9 1001 9

A 1010 10

B 1011 11

C 1010 12

D 1101 13

E 1110 14

F 1111 15

4 binary digits correspond to one hexadecimal digit

Page 7: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Number Representation Computer systems use the binary numerical system

Numbers need to be stored in fixed-size elements, such as the registers, hence they cannot be arbitrarily long

◦ The smallest addressable piece of memory is a byte - 8 bits◦ CPU registers are 32-bit or 64-bit long

Most Significant Bit (MSB) = Leftmost bit in number, the bit with the highest value

Least Significant Bit (LSB) = Rightmost bit in number, the bit with the smallest value

Page 8: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Binary Addition 1010

+1111

--------

Cheat sheet:

0+0=0

1+0=1

1+1=10

Column 20: 0+1=1.Record the 1. Temporary Result: 1; Carry: 0

Column 21: 1+1=10. Record the 0, carry the 1.Temporary Result: 01; Carry: 1

Column 22: 1+0=1 Add 1 from carry: 1+1=10. Record the 0, carry the 1.Temporary Result: 001; Carry: 1

Column 23: 1+1=10. Add 1 from carry: 10+1=11.Record the 11. Final result: 11001

Page 9: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Binary Multiplication 1010

x 11

---------

Cheat sheet

0x0=0

1x0=0

0x1=0

1x1=1

Multiplying by 2 is easy, just shift in (append) a 0 from the right

How about multiplying with 4 or 8?

Page 10: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Overflows Integer overflows occur when the result of an operation requires more bits than the length of the number involved

The overflowing bit is lost

Example with unsigned bytes 1001 1100 +0110 0111 ---------------- 1 0000 0011

Page 11: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Binary Division 111011 / 11 Same rules as in decimal division

When dividing integers the remainder is ignored

Page 12: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Bitwise NOT or One’s Complement

Bitwise operations are logical operations on the individual bits of one or two numbers

Performs logical negation of each bit in the number◦ Just flip each bit’s value

Example:

NOT 0100 0110 =

1011 1001

operand NOT operand

0 1

1 0

Page 13: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Bitwise AND Takes two numbers and performs the logical AND operation on each pair of corresponding bits

Example:

0100 0110

AND 0110 1100 =

0100 0100

operand operand’ AND result

0 0 0

0 1 0

1 0 0

1 1 1

Page 14: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Bitwise OR Takes two numbers and performs the logical OR operation on each pair of corresponding bits

Example:

0100 0110

OR 0110 1100 =

0110 1110

operand operand’ OR result

0 0 0

0 1 1

1 0 1

1 1 1

Page 15: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Bitwise XOR Takes two numbers and performs the logical XOR (eXclusive OR) operation on each pair of corresponding bits

Example:

0100 0110

XOR 0110 1100 =

0010 1010

operand operand’ OR result

0 0 0

0 1 1

1 0 1

1 1 0

Page 16: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Signed Numbers Sign-magnitude notation

◦ Use the leftmost bit of a number as the equivalent of a sign: “0” is “+”, “1” is “-”◦ Example: 12 0000 1100, -12 1000 1100

One’s complement◦ Flip all bits. The leftmost bit still indicates signed-ness◦ Example: 12 0000 1100, -12 1111 0011

Two’s complement◦ Flip all bits, and add 1. The leftmost bit still indicates signed-ness◦ Example: 12 0000 1100, -12 1111 0100

Excess 2(m-1)

◦ m is the length of the number in bits. Every number is represented by adding it to 2(m-1)

◦ Example: 12 27+12=140 1000 1100, -12 27-12=116 0111 0100◦ Numbers are the same as two’s complement with sign bit flipped

The largest signed number is smaller, than the largest unsigned number

Page 17: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Why So Many Ways? Let’s perform the following binary additions (-5+12), (-12+-5), and (12+-12)

Two’s complement makes the addition of both signed and unsigned integers

Different programming languages may use different ways of representing numbers

Java uses two’s complement for signed numbers

Sign-magnitude notation

One’s complement Two’s complement Excess 2(m-1)

12 0000 1100 0000 1100 0000 1100 1000 1100

-12 1000 1100 1111 0011 1111 0100 0111 0100

-5 1000 0101 1111 1010 1111 1011 0111 1011

Page 18: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Overflows and Signed Integers Integer overflows occur when the result of an operation requires more bits than the length of the number involved

The overflowing bit is lost

Example with unsigned bytes 1001 1100 +0110 0111 ---------------- 1 0000 0011

Example with signed bytes 0001 1100 +0110 0111 ---------------- 1000 0011

The overflow happens into the

sign bit

Page 19: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Shifting Bits The bits of a number can be moved or shifted to the left or right

Numbers are stored in fixed-size registers in the CPU◦ Moving bits can cause bits to shift-out and others to shift-in

Logical shift◦ Integers are treated as bit-strings◦ Appropriate for unsigned integers

Example: 1001 1000shift right 2 = 0010 0110

Example: 1001 1000shift left 3 = 1100 0000

Zeroes are shifted inShifted-out bits are lost

Page 20: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Shifting Bits The bits of a number can be moved or shifted to the left or right

Numbers are stored in fixed-size registers in the CPU◦ Moving bits can cause bits to shift-out and others to shift-in

Logical shift◦ Integers are treated as bit-strings◦ Appropriate for unsigned integers

Arithmetic shift◦ Integers are treated as numbers◦ Appropriate for signed integers

Example: 1001 1000shift right 2 = 1110 0110

Example: 1001 1000shift left 3 = 1100 0000

The leftmost bit is used when shifting-in bits from

the left

Zeroes are shifted inShifted-out bits are lost

Page 21: Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

Summary Numbers can be represented in different numerical systems

Computer use the binary system

Arithmetic operations like addition, multiplication, and division are straightforward

Bitwise operations perform actions on individuals bits of numbers

There are multiple ways to represent signed numbers◦ Two’s complement is the one used in most computers due to its simplicity

Numbers in the CPU have fixed length, so beware of overflows

Shift operations allow you to move bits in a number◦ The bits shifted-in from the left depend on whether we are performing an arithmetic or logical shift