27
NUMBER SYSTEMS

Number systems - binary, BCD, 2s comp

Embed Size (px)

DESCRIPTION

Number systems, including Binary, Sign/Magnitude, 2s Complement, Binary addition, Octal and BCD

Citation preview

Page 1: Number systems - binary, BCD, 2s comp

NUMBER SYSTEMS

Page 2: Number systems - binary, BCD, 2s comp

NUMBERS ARE EXPRESSED IN DIFFERENT WAYS

一二三

Chinesel ll lll

Roman

1 2 3Hindu-Arabic

Page 3: Number systems - binary, BCD, 2s comp

OUR NUMBER SYSTEM – DECIMAL OR DENARY SYSTEM

Adopted from the Hindu-Arabic numeral system

We use ten digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

4

Page 4: Number systems - binary, BCD, 2s comp

BINARY NUMBER SYSTEM

Number system based on 2

0 and 1 are used

0 = off

1 = on

As only 2 numbers are used, calculations can be performed quickly

Page 5: Number systems - binary, BCD, 2s comp

DENARY NUMBER SYSTEMWe learnt numbers by dividing into columns

Each heading is multiplied by 10

Thousands Hundreds Tens Units

2 0 1 4

1 9 8 5

0 5 2 3

0 0 3 1

0 0 0 8

1x1010x10100x10

Page 6: Number systems - binary, BCD, 2s comp

BINARY SYSTEM

128 64 32 16 8 4 2 1

1 0 0 0 0 0 0 1

Each heading is multiplied by 2

So, we have

One 128

None of the rest

And one 1

Therefore 128 + 1 = 129

1x22x24x28x216x232x264x2

Page 7: Number systems - binary, BCD, 2s comp

BINARY SYSTEM

128 64 32 16 8 4 2 1

0 0 0 0 0 0 0 1

Even if the number starts with 0, we usually include it:

This has none of the numbers except one 1

So the answer is 1

But we express 1 in binary as: 00000001

Page 8: Number systems - binary, BCD, 2s comp

NEGATIVE NUMBERS

So far we have only looked at positive numbers.

In 8-bit binary, there is nowhere to put a negative sign -

128 64 32 16 8 4 2 1

1 0 0 0 0 0 0 1

Page 9: Number systems - binary, BCD, 2s comp

SIGN / MAGNITUDE REPRESENTATION

So we replace the first column (128) and make it stand for +/-

+/- 64 32 16 8 4 2 1

We use a 1 to stand for the negative sign(-), and 0 if it’s a positive number (+)

So -75 is

+/- 64 32 16 8 4 2 1

1 1 0 0 1 0 1 1

Page 10: Number systems - binary, BCD, 2s comp

SIGN / MAGNITUDE REPRESENTATION

Imagine a line that divides the sign and the magnitude

+/- 64 32 16 8 4 2 1

SIGN

+/-

MAGNITUDE

Size of the number

Page 11: Number systems - binary, BCD, 2s comp

POSITIVE+/- 64 32 16 8 4 2 1

0 1 0 0 0 0 0 1

0 0 1 0 0 1 1 1

0 0 0 1 1 1 0 1

NEGATIVE+/- 64 32 16 8 4 2 1

1 1 0 0 0 0 0 1

1 0 1 0 0 1 1 1

1 0 0 1 1 1 0 1

Denary

65

39

29

Denary

-65

-39

-29

Page 12: Number systems - binary, BCD, 2s comp

PROBLEMS

Previously, we could use all columns and make a number up to 255.

Now, the biggest number we can make is halved, because we have replaced the 128 column with +/-.

Second, the binary number contains values AND ALSO a sign.

This makes it difficult to do arithmetic.

E.g. We can’t add a – sign to 1

+/- 64 32 16 8 4 2 1

1 1 0 0 1 0 1 1

Page 13: Number systems - binary, BCD, 2s comp

2s Complement

2s complement allows us to represent negative numbers without having to worry about using a sign instead of a number

In 2s complement we use -128 instead of -, which looks like this:

-128 64 32 16 8 4 2 1

Page 14: Number systems - binary, BCD, 2s comp

2s Complement

OK, se we want to represent -75 in 2s complement binary

As it’s a negative number, we need a 1 in the -128 column

-128 64 32 16 8 4 2 1

1

Page 15: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64

-128 64 32 16 8 4 2 1

1 0

Page 16: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32

-128 64 32 16 8 4 2 1

1 0 1

Page 17: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32, 1*16,

-128 64 32 16 8 4 2 1

1 0 1 1

Page 18: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32, 1*16, 0*8,

-128 64 32 16 8 4 2 1

1 0 1 1 0

Page 19: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32, 1*16, 0*8, 1*4,

-128 64 32 16 8 4 2 1

1 0 1 1 0 1

Page 20: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32, 1*16, 0*8, 1*4, 0*2

-128 64 32 16 8 4 2 1

1 0 1 1 0 1 0

Page 21: Number systems - binary, BCD, 2s comp

2s Complement

BUT, -128 is 53 too many (-128 – (-75) = -53)

This means we need to add 53 to get back to -75

So, 53 is 0*64, 1*32, 1*16, 0*8, 1*4, 0*2, 1*1

-128 64 32 16 8 4 2 1

1 0 1 1 0 1 0 1

Page 22: Number systems - binary, BCD, 2s comp

BINARY ADDITION

Adding two binary numbers isn’t really that scary – it’s just the same as adding denary numbers!

You need to work from right to left, just the same

Remember that there’s only 1 and 0, adding two 1s means you need to carry 1 to the next column

Page 23: Number systems - binary, BCD, 2s comp

DEMO

0 0 0 1 0 0 1 0

+ 0 0 0 1 0 1 0 0

00+0=0

1

1+0=1

11+0=1

0

0+0=0

01

1+1=0(carry 1)

10+0+1=1(1 was carried over)

000+0=0

Page 24: Number systems - binary, BCD, 2s comp

BINARY SUBTRACTION

Subtraction can cause problems, especially when subtracting a number from a smaller number.

75 – 14 is the same as 75 + (-14)

We use 2s complement to convert the numbers

-128 64 32 16 8 4 2 1

1 0 1 1 0 1 0 1

Page 25: Number systems - binary, BCD, 2s comp

OCTAL

Base of 8

We use 8 digits:

0,1,2,3,4,5,6,7

Headings become:

512 64 8 1

0 1 1 3

So, above we have

64+8+(3*1) = 75

Page 26: Number systems - binary, BCD, 2s comp

BINARY CODED DECIMAL (BCD)

Each denary digit is represented separately

Four binary digits are used (nibbles):

Binary Denary

8 4 2 1

0 1 1 1 = 4+2+1 = 7

0 1 0 1 = 4+1 = 5

1 0 0 1 = 8+1 = 9

Page 27: Number systems - binary, BCD, 2s comp

BINARY CODED DECIMAL (BCD)

Each denary digit is represented separately

Four binary digits are used:

Denary Binary

8 4 2 1

7 = 0 1 1 1

5 = 0 1 0 1

So, 75 in BCD is 01110101