36
Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor

Number Bases

Embed Size (px)

DESCRIPTION

Number Bases. Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor. Items for Today. Last week Digital logic, Boolean algebra, and circuits Logic gates and truth tables This Week Numbers and bases Working with binary. Number Base Systems. …. …. b 4. #. b 3. #. b 2. - PowerPoint PPT Presentation

Citation preview

Page 1: Number Bases

Number Bases

Informatics INFO I101

February 9, 2004

John C. Paolillo, Instructor

Page 2: Number Bases

Items for Today

• Last week– Digital logic, Boolean algebra, and circuits– Logic gates and truth tables

• This Week– Numbers and bases– Working with binary

Page 3: Number Bases

Number Base Systems

Page 4: Number Bases

The Format of a Base System

# # # # #… # # # # # …

b4 b3 b2 b1 b0… b-1 b-2 b-3 b-4 b-5 …

The number represented is the sum of all the products of the digit values and their respective place values

Page 5: Number Bases

Common Bases

• Decimal (Base 10)

• Binary (Base 2)

• Octal (Base 8)

• Hexadecimal (Base 16)

Page 6: Number Bases

Conversion to Base 10

• Identify each of the places in the new number base. These will correspond to the powers of the base, for example, with base 2, they are 1, 2, 4, 8, 16, 32, etc.

• Multiply the value for each place by the value of the digit appearing there;

• Add the results up, and you have the result in decimal

Note that if you divide and add correctly, you can reverse this procedure to convert decimal into another base. It’s harder, because you’re not used to using the appropriate addition and multiplication tables.

Page 7: Number Bases

Try out these examples

• What is 10011 Base 2 in decimal?

116+ 08 + 04 + 12 + 11 = 19• What is 121 Base 8 in decimal?

164 + 28 + 11 = 81• What is 247 Base 10 in Binary?

Here it helps to have a different procedure…

Page 8: Number Bases

Converting to Binary

24 23 22 21 2027 26 2528

16 8 4 2 1128

64 32256

247256 0247128 111964 15532 12316 1

78 074 132 1

1 0 1 1 10 1 1 1

11 1

What we’re converting

Page 9: Number Bases

Octal — base 8

• Sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7

7 = 111two = 7eight

• Octal values are usually not specially indicatedUnix example:chmod 666 myfile.html

Page 10: Number Bases

Octal Digits

Octal01234567

Binary000001010011100101110111

Decimal01234567

Page 11: Number Bases

Octal Tips

• each octal digit corresponds to three binary digits (bits)

• convert binary to octal by parsing each group of three bits into one octal digit

• convert octal to binary by translating each digit into three bits

• Examples:764eight = 111101100two

011011101two = 335eight

Page 12: Number Bases

Hexadecimal — base 16

• Sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

15 = 1111two = Fsixteen

• Hexadecimal (“hex”) values are usually indicated by a preceding base markerHTML: #FFFFFF

JavaScript, C: 0xF1AD

Page 13: Number Bases

Hexadecimal Digits

Hex0123456789ABCDEF

Binary0000000100100011010001010110011110001001101010111100110111101111

Decimal0123456789101112131415

Page 14: Number Bases

Hexadecimal Tips

• each hex digit corresponds to four binary digits (bits)

• convert binary to hex by parsing each group of four bits into one hex digit

• convert hex to binary by translating each digit into four bits

• Two hex digits make up one byte, a very common unit of memory

Page 15: Number Bases

IP Numbers

IP = “Internet Protocol”

129.79.142.114

dhcp-Memorial–142-114.memorial.indiana.edu

IP Number

Host Name

A Domain Name Server (DNS) has a database that matches IP and host name

Page 16: Number Bases

The IP Number

• Four Fields• 0-255 in each field• This is really base

256, but we use decimal numbers in each digit

129.79.142.114

Net

Subnet

Node

Page 17: Number Bases

Binary Addition

Page 18: Number Bases

Adding in Binary

• Zero plus any other values leaves that valueIdentity value for addition

No carry is generated

• One plus one leaves zero and causes a carry (one) to the next digitEach successive digit must accept the carry from

the previous

Page 19: Number Bases

Try these calculations

01001101 Base 2

+ 00011011 Base 2

01111111 Base 2

+ 00000001 Base 2

102 Base 8

+ 121 Base 8

01001101 Base 2

00000100 Base 2

0000101 Base 2

0000101 Base 2

01111111 Base 2

00011011 Base 2

Page 20: Number Bases

Addition: Truth Tables

CI00001111

A01010101

B00110011

S COS01101001

CO00010111

Page 21: Number Bases

Addition: Half Adder

+ 00 01

00 00 01

01 01 10

S

A B

XOR 0 1

0 0 1

1 1 0

C

The half adder sends a carry, but can’t accept oneSo we need another for the carry bit

0 1

0 0 0

1 0 1

Page 22: Number Bases

Addition in Binary

• Two half-adders gives us a full adder– two inputs plus carry

• Adders are cascaded to permit adding binary numbers– Eight adders allows adding (0...256) + (0...256) in

binary numbers

– Overflow can happen (200 + 100)

• Binary adders are used to do other computations as well...

Page 23: Number Bases

Subtraction

Complement Representations

Page 24: Number Bases

Subtraction

– 00 01

00 00 01

01 –01 00

• Subtraction is asymmetrical

• That makes it harder• We have to borrow

sometimes

 827 Minuend–223 Subtrahend=604 Difference/remainder

Page 25: Number Bases

When Subtraction is Easy

456–123333

999–123876

• Subtraction is easy if you don’t have to borrow– i.e. if all the digits of the minuend

are greater than (or equal to) all those of the subtrahend

• This will always be true if the minuend is all 9’s: 999, or 999999, or 9999999999 etc.

Page 26: Number Bases

Using Easy Subtraction

• Subtract the subtrahend from 999 (or whatever we need) (easy)

• Add the result to the minuend (ordinary addition)

• Add 1 (easy)• Subtract 1000 (drop highest digit)Difference = Minuend + 999 – Subtrahend +

1 – 1000

This works for binary as well as decimal

Page 27: Number Bases

Easy Subtraction in Binary

• Subtract the subtrahend from 111 (or whatever we need) (easy)

• Add the result to the minuend (ordinary addition)

• Add 1 (easy)• Subtract 1000 (drop highest digit)Difference = Minuend + 111 – Subtrahend +

1 – 1000

Page 28: Number Bases

Binary Subtraction Example

10010101–01101110?????????

11111111–0110111010010001

This is the same as inverting each bit

+ 10010101100100110

+1100100111

–10000000000100111

Regular addition

Add one

Now drop the highest bit (easy: it’s out of range)

00100111

Page 29: Number Bases

Subtraction Procedure

Invert each bit

Regular addition

Add one

Now drop the highest bit (easy: it’s out of range)

Each of these steps is a simple operation we can perform using our logic circuits

Bitwise XOR

Cascaded Adders

Add carry bit

Drop the highest bit ( it overflows)

Page 30: Number Bases

Negative Numbers

Invert each bit

Add one

These steps make the negative of a number in twos-complement notation

• Twos complements can be added to other numbers normally• Positive numbers cannot use the highest bit (the sign bit)• This is the normal representation of negative numbers in binary

Page 31: Number Bases

Negative Numbers in Binary

00000000 000000001 100000010 200000011 300000100 400000101 500000110 600000111 700001000 800001001 9

etc.

11111111 –111111110 –211111101 –311111100 –411111011 –511111010 –611111001 –711111000 –811110111 –911110110 –10etc.

Page 32: Number Bases

Representations

• The number representation you use (encoding) affects the way you need to do arithmetic (procedure)

• This is true of all codes: encoding (representation) affects procedure (algorithm)

• Good binary codes make use of properties of binary numbers and digital logic

Page 33: Number Bases

A problem

A computer program adds 20,000 and 20,000 and instead of 40,000, it reports –25,566

• No errors in encoding, decoding or addition

• How? Because the result is a negative number in twos-complement notation (highest bit = sign bit)

Page 34: Number Bases

How it works

• 20,000 base ten is 0100111000100000 binary010011100010000001001110001000001001110001000000

• Highest bit is set, so number is negative in twos complement notation: subtract one and invert to display1001110001000000 – 1 = 1001110000111111

0110001111000000 = 25,566

Page 35: Number Bases

Bottom Line

• Representations themselves, as we use them, have limits.

• Interpretation depends on context

• two procedures (encoding/decoding and addition) may be in and of themselves correct, but conflict in their application to specific examples

Page 36: Number Bases

MER has landed