1-1 Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16 Conversion between number...

20
1-1 Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16 Conversion between number systems. Signed numbers, 2’s complements binary and binary fractions. Floating point formats. Simple arithmetic operations using binary number. Bits, bytes and words

Transcript of 1-1 Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16 Conversion between number...

1-1

Number Systems

Denary Base 10 Binary Base 2 Hexadecimal Base 16 Conversion between number systems. Signed numbers, 2’s complements binary and binary

fractions. Floating point formats. Simple arithmetic operations using binary number. Bits, bytes and words

1-2

Denary Base 10

The base-10 system (decimal) is the most commonly used today ( 0 1 2 3 4 5 6 7 8 9)

To represent the number four thousand three hundred and twenty one (4321)

Thousand Hundreds Tens Units

103 102 101 100

1000 100 10 1

4×1000 3×100 2×10 1×1

4000 +300 +20 1

Total 4321

1-3

Binary Base 2

The base-2 system (Binary) the binary system is used internally by virtually all modern computers. ( ‘0’ or ‘1’ )

0 1 0 1 1 1 0 1

27 26 25 24 23 22 21 20

127 64 32 16 8 4 2 1

0×127 1×64 0×32 1×16 1×8 1×4 0×2 1×1

0 +64 +0 +16 +8 +4 +0 +1

Total 93

1-4

Hexadecimal Base 16

The base-16 system (Hexadecimal) uses the numerals “0 1 2 3 4 5 6 7 8 9 B C D E F”

Easy to represent binary code (form of shorthand) in which one hexadecimal digit stands in place of four binary bits.

F A

161 160

16 1

15×16 10×1

240 +10

Total 240

Hex f a Binary ,1111 1010

1-5

Converting from denary to binary 1) Start with the number you want (44) 2) Divide by the base you want to convert to (2) 3) Calculate the remainder from the sum 4) Place result of the sum on the next row

Number Base required Remainder

44 ÷ 2 0

22 ÷ 2 0

11 ÷ 2 1

5 ÷ 2 1

2 ÷ 2 0

1 ÷ 2 1

44 denary = 101100 binary

1-6

Converting from denary to hex

1) Start with the number you want (4321) 2) Divide by the base you want to convert to (16) 3) Calculate the remainder from the sum 4) Place result of the sum on the next row

Number Base required Remainder

4321 ÷ 16 1

270 ÷ 16 E (14)

16 ÷ 16 0

1 ÷ 16 1

4321 Denary = 10E1 Hexadecimal

1-7

Denary, Hex & BinaryDenary Hex Binary

0 0 0 0 0 0

1 1 0 0 0 1

2 2 0 0 1 0

3 3 0 0 1 1

4 4 0 1 0 0

5 5 0 1 0 1

6 6 0 1 1 0

7 7 0 1 1 1

8 8 1 0 0 0

9 9 1 0 0 1

10 A 1 0 1 0

11 B 1 0 1 1

12 C 1 1 0 0

13 D 1 1 0 1

14 E 1 1 1 0

15 F 1 1 1 1

1-8

Signed numbers

In mathematics negative numbers can be prefixed by the '-' symbol.

On a computer system several methods cane be used Use of a sign bit Often Most Significant bit is used for the sign

Number Binary

7 0 1 1 1

......1 0 0 0 1

0 0 0 0 0

-0 1 0 0 0

-1......... 1 0 0 1

-7 1 1 1 1

4 Bits gives a range -7 to +7

Two representations of '0'

1-9

1's complement

To obtain the negative number the positive binary value is complemented

'0' replaced with '1' and '1' replaced with '0' For example binary 00001010 (decimal 10)

complemented becomes 11110101

1-10

2's complement

To obtain the 2's complement of a number First the 1's complement is obtained Then 1 is added i.e. decimal 10 = 00001010, complemented =

11110101 add 1 = 11110110. Advantage of 2's complement is that no

separate addition and subtraction circuits are required

1-11

Binary fractions

So far only integer numbers have been considered

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sign 26 25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8

Whole Number Fraction

Decimal 10.75 would be 1010.11 Limited by the number of bits assigned to whole

number Accuracy is lost smallest value is 0.00390625

1-12

Floating point numbers

In scientific applications we often need very small or large numbers

In the floating point system number is represented by a signed fractional component called the mantissa and a signed exponent.

mantissa × 10exponent

The value is normalised i.e. 1230000.0 would be become 0.123×107

In a typical system where a float is 32 bits 24 bits hold the signed mantissa and 8bits for the exponent

Floating point calculations often carried out in a separate co processor

1-13

Binary arithmetic

Binary addition

A op B Sum Carry

0 + 0 0 0

0 + 1 1 0

1 + 0 1 0

1 + 1 0 1

Dec Binary

10 1 0 1 0

+9 1 0 0 1

19 1 0 0 1 1

1-14

Binary arithmetic

Binary subtraction

A op B Sum borrow

0 - 0 0 0

0 - 1 1 1

1 - 0 1 1

1 - 1 0 0

* * * * (starred columns are borrowed from) 1 1 0 1 1 1 0 − 1 0 1 1 1 ---------------- = 1 0 1 0 1 1 1

1-15

Binary arithmetic Multiplication - Two numbers A and B can be multiplied by partial

products: for each digit in B, the product of that digit in A is calculated and written on a new line, shifted leftward so that its rightmost digit lines up with the digit in B that was used. The sum of all these partial products gives the final result.

1 0 1 1 (A) (decimal 11)× 1 0 1 0 (B) (decimal 10)

--------- 0 0 0 0 ← Corresponds to a zero in B + 1 0 1 1 ← Corresponds to a one in B + 0 0 0 0 + 1 0 1 1 --------------- = 1 1 0 1 1 1 0 (decimal 110)

1-16

Binary arithmetic

Division i.e. divide 0110101 (53) by 0101 (5).

___ 1010 (10 answer) 0101| 0110101

0101 110

0101 11 (3 remainder)

1-17

Bits Bytes & Words

Bit either a '1' or a '0' Byte comprises 8 bits ' 10110011' Nibble - Half a byte (4 bits) '1011' Word Normally the bus width of the

microprocessor – in this case 32 bits Hex numbers in the keil compiler are prefixed by

'0x' (0xa7) Floating point numbers – exponent is

represented by the letter 'e' (1.25e-3)

ASCII character codes Integer numbers are easily represented in

binary. This is not the case for characters and other punctuation marks.

The standard ASCII code defines 128 character codes (from 0 to 127), of which, the first 32 are control codes (non-printable), and the other 96 are represent printable characters.

The ASCII code requires a 7 bit binary code. Memory is usually a 8-bit wide which leaves 1 unused bit – use to provide additional characters called Extended ASCII. Not standardised!

The Standard ASCII Table* 0 1 2 3 4 5 6 7 8 9 A B C D E F

0 NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI

1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US

2   ! " # $ % & ' ( ) * + , - . /

3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?

4 @ A B C D E F G H I J K L M N O

5 P Q R S T U V W X Y Z [ \ ] ^ _

6 ` a b c d e f g h i j k l m n o

7 p q r s t u v w x y z { | } ~   �

* This table is organized to be easily read in hexadecimal: row numbers represent the first digit and the column numbers represent the second one. For example, the A character is located at the 4throw and the 1st column, for that it would be represented in hexadecimal as 0x41 (6510).

Unicode ASCII only supports 128 characters ASCII was extended to 255 characters using the

iso 8859 method, where 15  regions/language groups were defined.

Unicode method contains all the worlds characters (well almost) in one coding list.

Uses multibyte encoding of 1,2 or 4 bytes Common coding is UTF-8

first 128 codes same as ASCII so provides backwards compatibility

See http://www.unicode.org