34
Binary & Hex The number systems of Computer Science

Binary & Hex The number systems of Computer Science

Embed Size (px)

Citation preview

Page 1: Binary & Hex The number systems of Computer Science

Binary & Hex

The number systems ofComputer Science

Page 2: Binary & Hex The number systems of Computer Science

What you will learn

DecimalBinaryHexadecimalArithmeticConverting between number basesCounting2’s Compliment

Page 3: Binary & Hex The number systems of Computer Science

Number Systems

PrehistoryUnary, or marks: /

/////// = 7/////// + ////// = /////////////

Babylonian (Mesopotamia) around 2000 BC Used 60 numerical symbols – sexadecimal. Used to measure time5; 25, 30 = 5hr 25min 30 secs. Was positional based by had no concept

of zero. 5,4560 = 5 x 60 + 45 x 1 = 34510

Roman Numerals: (non positional, no concept of zero)VII + V = VVII = XIIMCMXCIX = 1999MCMXCVIIII = 1999MCMLXXXXVIIII = 1999MDCCCCLXXXXVIIII = 1999

Page 4: Binary & Hex The number systems of Computer Science

Quote from Pierre Simon Laplace

“The ingenious method of expressing every possible number using a set of ten symbols (each symbol having a place value and an absolute value) emerged in India. The idea seems so simple nowadays that its significance and profound importance is no longer appreciated. Its simplicity lies in the way it facilitated calculation and placed arithmetic foremost amongst useful inventions. the importance of this invention is more readily appreciated when one considers that it was beyond the two greatest men of Antiquity, Archimedes and Apollonius. “

Page 5: Binary & Hex The number systems of Computer Science

Arabic/Indian Numerals (Decimal)

Better, Arabic/Indian based Numerals:7 + 5 = 12 = 1 x 10 + 2

345 is really 3 x 100 + 4 x 10 + 5 x 1 3 x 102 + 4 x 101 + 5 x 100

3 is the most significant symbol (carries the most weight)

5 is the least significant symbol (carries the least weight)

Digits (or symbols) allowed: 0-9Base (or radix): 10

Page 6: Binary & Hex The number systems of Computer Science

•Try multiplication in (non-positional) Roman numerals(!):

XXXIII (33 in decimal) XII (12 in decimal)---------XXXIIIXXXIIICCCXXX-----------CCCXXXXXXXXXIIIIIICCCLXXXXVICCCXCVI = 396

•The Babylonians wouldn’t have had this problem.

Page 7: Binary & Hex The number systems of Computer Science

•There are many ways to “represent” a number•Representation does not affect computation result

LIX + XXXIII = LXXXXII (Roman)59 + 33 = 92 (Decimal)

•Representation affects difficulty of computing results•No concept of “Zero”•Computers need a representation that works with fastelectronic circuits

Page 8: Binary & Hex The number systems of Computer Science

What is the base ?

The decimal numbering system is also known as base 10. The values of the positions are calculated by taking 10 to some power.

Why is the base 10 for decimal numbers ? Because we use 10 digits. The digits 0

through 9.

Page 9: Binary & Hex The number systems of Computer Science

Main Memory

Original computers used holes/no hole to represent two state, 1/0.Modern computers use transistor on/off translates to values 1/0Requires use of Binary number system

Page 10: Binary & Hex The number systems of Computer Science

Binary: positional numbers work great with 2-state devices

•Digits (symbols) allowed: 0, 1•Binary Digits, or bits

•Base (radix): 2•10012 is really

•1 x 23 + 0 x 22 + 0 X 21 + 1 X 20

•910

•110002 is really•?•?

•Computers usually multiply Arabic numerals by convertingto binary, multiplying and converting back for display

Page 11: Binary & Hex The number systems of Computer Science

Counting in Binary

Binary 0 1 10 11 100 101 110 111

Decimal equivalent 0 1 2 3 4 5 6 7

Page 12: Binary & Hex The number systems of Computer Science

Decimal to Binary

•Divide decimal value by 2 until the value is 0E.g.

47 /2 = 23 Remainder 123/2 = 11 Remainder 111/2 = 5 Remainder 15/2 = 2 Remainder 12/2 = 1 Remainder 01/2 = 0 Remainder 1

Read from the bottom up

Ans: 101111

Page 13: Binary & Hex The number systems of Computer Science

Binary to Decimal

1 0 0 0 0 0 1 1 X 20 = 1

26 25 24 23 22 21 20 0 X 21 = 0

0 X 22 = 0

20 = 1 24 = 16 0 X 23 = 0

21 = 2 25 = 32 0 X 24 = 0

22 = 4 26 = 64 0 X 25 = 0

23 = 8 1 X 26 = 64

65

Page 14: Binary & Hex The number systems of Computer Science

Binary to Decimal (alternative)

Converting the binary 100101 to 37

Multiply running total by two, add in next digit. Repeat for all digits.

Page 15: Binary & Hex The number systems of Computer Science

Some buzz words• Single Binary Digit =Bit• 4 bits = nibble• 8 bits = byte• 16 bits = word• 32 bits = longword• 2 nibbles = 1 byte• 2 bytes = 1 word

The word size depends on the processor used, above is shown the word size for the 68000

Page 16: Binary & Hex The number systems of Computer Science

Addition of Binary Numbers

Examples:

1 0 0 1 0 0 0 1 1 1 0 0 + 0 1 1 0 + 1 0 0 1 + 0 1 0 1 1 1 1 1 1 0 1 0 1 0 0 0 1

Carry a one

Page 17: Binary & Hex The number systems of Computer Science

Addition (large numbers)

Example showing larger numbers:

1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 + 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0

Page 18: Binary & Hex The number systems of Computer Science

Working with large numbers

0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1

Humans can’t work well with binary numbers. We will make errors.

Shorthand for binary that’s easier for us to work with - Hexadecimal

Page 19: Binary & Hex The number systems of Computer Science

Hexadecimal

Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1

Hex 5 0 9 7

Written: 509716

Page 20: Binary & Hex The number systems of Computer Science

What is Hexadecimal really ?

Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1

Hex 5 0 9 7

A number expressed in base 16. It’s easy to convert binary to hex and hex to binary because 16 is 24.

Page 21: Binary & Hex The number systems of Computer Science

Counting in Hex Binary Hex Binary Hex 0 0 0 0 0 1 0 0 0 8 0 0 0 1 1 1 0 0 1 9 0 0 1 0 2 1 0 1 0 A 0 0 1 1 3 1 0 1 1 B 0 1 0 0 4 1 1 0 0 C 0 1 0 1 5 1 1 0 1 D 0 1 1 0 6 1 1 1 0 E 0 1 1 1 7 1 1 1 1 F

Page 22: Binary & Hex The number systems of Computer Science

Decimal to Hex

Repeat dividing by 16 and noting the remainder until the quotient = 0

E.g. 54401d to Hex54401/16 = 3400 Remainder 13400/16 = 212 Remainder 8212/16 = 16 Remainder 413/16 = 0 Remainder 13

Remainders are expressed Hex numbersAnswer: $D481

Page 23: Binary & Hex The number systems of Computer Science

Hex to Decimal

Write out the 1st 16 numbersSame as for binary to decimalE.g. F9E116

1x160 = 1Ex161 =14x16 = 2249x162 =9x256= 2304Fx163 =15x4096= 61440

= 63969

Page 24: Binary & Hex The number systems of Computer Science

Another Binary to Hex Conversion

Binary 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1

Hex 7 C 3 F

7C3F16

Page 25: Binary & Hex The number systems of Computer Science

Binary Coding Schemes

To represent characters we use Binary Coding SchemesTwo of the most popular are

ASCII (American Standard Code for Information Interchange EBCDIC (Extended Binary Coded Decimal Interchange Code)

Here are some ASCII codes CHARACTER ASCII A 01000001 – 41 in hex B 01000010 - 42 in hex C 01000011 - 43 in hex D 01000100 - 44 in hex BELL 00000111 – 7 in hex ESC 00011101 – 1B in hex

Page 26: Binary & Hex The number systems of Computer Science

Binary Coded Decimal (BCD)

Each digit is converted individually to it’s binary equivalent 2634 = 0010 0110 0011 01003825 = 0011 1000 0010 0101Disadvantages are that arithmetic is more complicated and storage is inefficent.Used only in simple device where memory is not a problem e.g. Calculators, clocks, etc.

Page 27: Binary & Hex The number systems of Computer Science

Negative Integers

Most humans precede number with “-” (e.g., -2000) Accountants, however, use

parentheses: (2000)

Sign-magnitudeExample: -1000 in hex? 100010 = 3 x 162 + e x 161 + 8 x 160

-3E816

Page 28: Binary & Hex The number systems of Computer Science

2’s Compliment

Used for negative binary numbersProcedure:Invert all the bits: 0 becomes 1 and 1 becomes 0Add 1

E.g. To get –1110 = 000000012

Invert all bits 11111110Add 1 11111111Ans: -110 = 111111112

Page 29: Binary & Hex The number systems of Computer Science

2’s Compliment

Using the TC notation, the Most significant indicates the sign. 1 means a negative numberWith 8 bits, can represent signed numbers in the range:+127 -1 0 -12801111111 11111111 00000000 10000000

With N bits, can represent signed numbers in the range:(2N-1)-1 0 -(2N-1)

TC is widely used because it is easy to manipulate. The sign bit is treated the same as the others

Page 30: Binary & Hex The number systems of Computer Science

2s Compliment Clock

Notice how when we add one to a negative number it’s values move closer to zero

Stepping clockwise increases the binary number

Stepping anti-clockwise decreases the binary number

Page 31: Binary & Hex The number systems of Computer Science

Addition & Subtraction

Addition of TC’s numbers same as normal binary additionSubtraction of TC’s numbers can use either A-B or A + TC(B)

E.g.01112 – 00112 = ???

Change second number to TC01111101+0100Answer: 01112 – 00112 = 01002

Page 32: Binary & Hex The number systems of Computer Science

Decimal fractions to binaryMore info on p158 Clements.

Question: Convert 0.6250 (decimal) to a binary fraction

0.3125 x 2 = 0.6250 yielding 0

0.625 x 2 = 1.2500 yielding 1

0.25 = 0.5000 yielding 0

0.5 x 2 = 1.0000 yielding 1

Ans:0.0101

Page 33: Binary & Hex The number systems of Computer Science

Binary to Decimal fractions

More info on p158 Clements.

Page 34: Binary & Hex The number systems of Computer Science

Summary

DecimalBinaryHexadecimalArithmeticConverting between number basesCountingCoding schemes2’s Compliment