Sie sind auf Seite 1von 18

ELE 107-Week 2: Number Systems & Binary Codes

Number Systems
We use numbers in daily life as a quantization
tool. A numeral is an expression (symbol) that
represents a number.
Numbers are abstractions that in turn represent
quantity: count, length, money, mass, duration
of time, etc.
Unfortunately, numbers can be represented in
many different ways. Also, a number may have
extra information associated with it, which
increases the choice of ways to represent it.

Number Systems
ELE 107
Computers and Programming I

ELE 107 Dr. Derya Altunay

Number Systems

As another example, consider the representation


of the number 11, which has no associated units.
Some different ways of representing 11 might be:
11 , XI , eleven , 11.00 ,
The choices depend on the context. The two
representations 11 and 11.00 tell us that there are
also different types of numbers: whole numbers
and fractions.

It clearly refers to a number. It involves a numeric


measure of quantity, and requires some choice of
units.
So, we might say the fuel capacity is: 88,828
gallons, or 336,250 liters, or 606,701 pounds.

Each representation describes the same amount


of fuel capacity, but because different units are
used, different numbers become involved.
3

Number Systems

ELE 107 Dr. Derya Altunay

Number Systems
In Roman number system a symbol always
represents the same weight:

Number systems may be symbolic,


such as the Roman number system, or
they may be weighted-positional, such as the Arabic
number system we normally use.

I = 1 , V = 5 , X = 10 , L = 50 , C = 100
D = 500 , M = 1000.

If a symbol is repeated or lies to the right of


another symbol, then the value of the symbol is
additive. (CXV is ..).
If a symbol is repeated or lies to the left of a
higher-valued symbol, then the value of the
symbol is subtractive. (XXC is ..).

Modern number systems are weighted-positional


number systems.

ELE 107 Dr. Derya Altunay

Number Systems

As an example, consider the description the


fuel capacity of an Airbus A380.

ELE 107 Dr. Derya Altunay

ELE 107 Dr. Derya Altunay

ELE 107 Dr. Derya Altunay

ELE 107-Week 2: Number Systems & Binary Codes

Number Systems

Number Systems

In a weighted-positional number system, the


value of a particular digit depends on its
position in the sequence of digits relative to a
radix (base) point.
Number is a concept independent of its representation; any given number can be zero, an
integer, fractional, irrational, transcendental,
negative, prime, or complex depending on the
underlying base system.
7

ELE 107 Dr. Derya Altunay

Number Systems

(734)14

4142 + 3141 + 7140

7142 + 3141 + 4140

4196 + 314 + 71
78410 + 4210 + 710

7196 + 314 + 41
137210 + 4210 + 410

(833)10

7102 + 3101 + 4100


7100 + 310 + 41
700 + 30 + 4

437

734

ELE 107 Dr. Derya Altunay

(N)r = xn xn-1 xn-2 x1 x0 . x-1 x-2 x-m


9

Number Systems

ELE 107 Dr. Derya Altunay

10

Number Systems
General form:
Position:
n n-1 2 1 0 . -1 -2 -m
Position value: r n r n-1 r2 r1 r0 . r -1 r -2 r -m
Number:
xn xn-1 x2 x1x0 . x-1 x-2 x-m

(N)r = xn xn-1 xn-2 x1 x0 . x-1 x-2 x-m


integer part radix fractional part
point

where,
r is the base of the number N,
xn is the most significant digit (MSD),
x-m is the least significant digit (LSD).

ELE 107 Dr. Derya Altunay

4102 + 3101 + 7100


4100 + 310 + 71
400 + 30 + 7

A weighted-positional number system can be


constructed around any integral base. Most
common ones used are: radix 10, radix 2,
radix 8, radix 16.
A number in any weighted-positional number
system can be represented using a string of
digits (symbols) :

(1418)10

ELE 107 Dr. Derya Altunay

734

Number Systems

If the numbers are given in a different basis:


(437)14

437

There are r digits (symbols) in a base-r number system:


{ 0, 1, 2 , , r-1 }
The position values (weights) are powers of the base.
Each digit has a value relative to its position in the
number.
Any number greater than r can be described by a
combination these digits.
11

ELE 107 Dr. Derya Altunay

12

ELE 107-Week 2: Number Systems & Binary Codes

Number Systems

Representation of Numbers
Each number has a
unique representation.
Counting:
When you run out of
digits, make it a zero
and increment the next
place value to the left.
99 becomes 100

(N)r = xn xn-1 xn-2 x1 x0 . x-1 x-2 x-m


= xn r n + +x1r1 +x0r0 +x-1 r -1 + x-m r -m
= (M)10

13

ELE 107 Dr. Derya Altunay

Uses two symbols: 0, and 1. (Each is called a bit for


binary digit)
The weights of each position are powers of two.
A number such as (101.01)2 will be expanded as:
2
22
1

-1 -2
10 -1 10 -2
3
5

15

ELE 107 Dr. Derya Altunay

0
20
1

-1 -2
2 -1 2 -2
0
1

16

ELE 107 Dr. Derya Altunay

Numbers with Different Bases

Number Systems

Decimal
Decimal: Base 10
00
Binary: Base 2
01
02
Octal: Base 8
03
Hexadecimal: Base 16
04
Octal and Hex forms are 05
06
more convenient for
07
humans to use
08
Dont have to deal with
09
long and confusing strings 10
11
of 1s and 0s
12
Most computer manuals use13
either octal or hex numbers 14

Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
15
1111

(1234)10 = 1 x 103 + 2 x 102 + 3 x 101 + 4 x 100


(1010)2 = 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20
=1x8 + 0x4 + 1x2 + 0x1
= 810 + 210 = (10)10

ELE 107 Dr. Derya Altunay

1
21
0

= (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2)


= 5.25 in the decimal system.

= (6 x 102) + (4 x 101) + (2 x 100) + (3 x 10-1) + (5 x 10-2)


= 642.35 in the decimal system.

General formula for converting


a number from base r to decimal
(xk is the kth digit of the number)

14

Binary number system (base-2):

Uses ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.


The positional values (weights) of each position are
powers of ten.
A number such as (642.35 )10 will be expanded as:
0
100
2

When you run out of


digits, make it a zero and
increment the next place
value to the left.
11two becomes 100two

Binary Numbers

Decimal number system (base-10):

1
101
4

Each number has a


unique representation.
Counting:

ELE 107 Dr. Derya Altunay

Decimal Numbers

2
102
6

Binary

Decimal

A number in any weighted-positional number


system can be easily converted to an equivalent
decimal number by expressing the number as a
polynomial in powers of the radix:

D = xk r k
k = -m

17

ELE 107 Dr. Derya Altunay

Octal Hexadecimal
00
0
01
1
02
2
03
3
04
4
05
5
06
6
07
7
10
8
11
9
12
A
13
B
14
C
15
D
16
E
17
F
18

ELE 107-Week 2: Number Systems & Binary Codes

Numbers with Different Bases


It takes 3 binary digits Decimal
00
(bits) to represent the
01
numbers 0-7
02
Each group of 3 bits
03
04
corresponds to exactly
05
one octal digit
06
It takes 4 (bits) to
07
represent the numbers
08
09
0-15
10
Each group of 4 bits
11
corresponds to exactly
12
one hex digit
13

Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
14
1110
15
1111

Octal Numbers

Octal Hexadecimal
00
0
01
1
02
2
03
3
04
4
05
5
06
6
07
7
10
8
11
9
12
A
13
B
14
C
15
D
16
E
17
F
19

ELE 107 Dr. Derya Altunay

Hexadecimal number system (base-16):

0
160

1
81
4

0
80
2

26 25 24 23 22 21 20
64+0+16+0+0+2+1 = (83)10

21

Binary-to-Decimal Conversion

ELE 107 Dr. Derya Altunay

20
21
22
23
24
25
26
27
28
29
210
211
212
213
214
215
216

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536

1K
2K
4K
8K
16K
32K
64K

22

Decimal-to-Binary Conversion

(1011.11)2 = 123 + 022 + 121 + 120 + 12-1 +


12-2
= 8 + 0 + 2 + 1 + + = (11.75)10

Let N be a decimal integer:

Repeated
Subtraction

1. Find the greatest integer that is a power of 2 and


when subtracted from N it yields a positive
difference N1
2. Place a 1 in the MSB
3. Repeat Step 1, starting from N1 and finding
difference N2 . Place a 1 in the corresponding bit.
Stop when the difference is zero.

2
1
0
-1
-2
(100.101)
2 = 12 + 02 + 02 + 12 + 02 + 12
3

= 4 + 0 + 0 + + 0 + = (4.625)10
(111.10)2 = 122 + 121 + 120 + 12-1 + 02-2
= 4 + 2 + 1 + + 0 = (7.5)10
(011.001)2 = 022+121+120+02-1+02-2+12-3
= 0 +2 + 1 + 0 + 0 + = (3.125)10
ELE 107 Dr. Derya Altunay

20

ELE 107 Dr. Derya Altunay

= (6 x 162) + (4 x 161) + (15 x 160) + (3 x 16-1) + (0 x 16-2)


= (1615.1875)10 in the decimal system.
ELE 107 Dr. Derya Altunay

-1 -2
8 -1 8 -2
3
0

= (6 x 82) + (4 x 81) + (2 x 80) + (3 x 8-1) + (0 x 8-2)


= (418.375)10 in the decimal system.

1010011

-1 -2
16 -1 16 -2
3

2
82
6

Each bit in a binary number


refers to a power of two:

Uses sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,


B, C, D , E and F.
The positional values (weights) of each position are
powers of sixteen.
A number such as (64F.30 )16 will be expanded as:
1
161

Uses eight symbols: 0, 1, 2, 3, 4, 5, 6, and 7.


The positional values (weights) of each position are
powers of eight.
A number such as (642.30 )8 will be expanded as:

Binary-to-Decimal Conversion

Hexadecimal Numbers

2
162

Octal number system (base-8):

23

ELE 107 Dr. Derya Altunay

24

ELE 107-Week 2: Number Systems & Binary Codes

Decimal-to-Binary Conversion
Start with decimal integer N = (25)10

Decimal-to-Binary Conversion

Repeated
Subtraction

Subtract largest power of two N


25 - 24 = 25 - 16 = 9
Subtract largest power of two Difference
9 - 23 = 9 - 8 = 1
Subtract largest power of two Difference
1 - 20 = 1 - 1 = 0
Now, we see (25)10 = 24 + 23 + 20 = (1 1 0 0 1)2

1. Divide the integer successively by 2


until you get to zero
2. Read the remainders in reverse order.

Repeated
Subtraction

+ 23 + + 21 + 20
1 0 1 1)2
26

ELE 107 Dr. Derya Altunay

Decimal-to-Binary Conversion
Let N be a decimal integer:

512 = 29
128 = 27
64 = 26
8 = 23
2 = 21
1 = 20

715 512 = 203 = N1


203 128 = 75 = N2
75 64 = 11 = N3
11 8 = 3 = N4
3 2 = 1 = N5
1 1 = 0 = N6
(715)10 = 29 +
27 + 26
=(1 0 1 1 0

25

ELE 107 Dr. Derya Altunay

Let N = (715)10

Decimal-to-Binary Conversion
Let N= (109)10
Repeated
109/2 = 54 remainder 1 (l.s.b)
Division
54/2 = 27 remainder 0
27/2 = 13 remainder 1
13/2 = 6 remainder 1
Read
6/2 = 3 remainder 0
backwards
3/2 = 1 remainder 1
1/2 = 0 remainder 1 (m.s.b)

Repeated
Division

(109)10 = (1101101)2
27

ELE 107 Dr. Derya Altunay

28

ELE 107 Dr. Derya Altunay

Binary Octal

Binary Hexadecimal

Each octal digit is represented by three bits.


3 bits per group starting from the radix point.

Each hex digit is represented by four bits.


4 bits per group starting from the radix point.

(011 100 111 . 001 010 110 101)2

(0010 0100 1100 . 1010 0111 1111 1101)2

(3

( 2

7 . 1

5 )8

( 10 110 110 . 1011)2

(2

( 2

ELE 107 Dr. Derya Altunay

6 . 5

4 )8
29

C .

D )16

10 1011 0011 . 1010 0111 110)2


B

ELE 107 Dr. Derya Altunay

3.

C )16
30

ELE 107-Week 2: Number Systems & Binary Codes

Octal Hexadecimal

Decimal to any radix r

Go through decimal:
Hex Decimal Octal
Octal Decimal Hex

Conversion from decimal to radix r system is


carried out by separating the number into its
integer and fraction parts and converting
each part separately.
Divide the integer successively by r and collect
the remainders backwards (read up).
Multiply the fraction successively by r , keep
track of the integral part until the fraction
becomes zero, and read down.

Go through binary ( much easier! )


Hex Binary Octal
Octal Binary Hex

31

ELE 107 Dr. Derya Altunay

Decimal to Binary
(6.6875)10 = ( ? )2 = (110.1011)2
Integer part:
6/2 = 3 remainder 0 (l.s.b)
3/2 = 1 remainder 1
1/2 = 0 remainder 1 (m.s.b)
Fraction part:
0.6875 x 2 = 1.3750 (m.s.b)
0.3750 x 2 = 0.7500
0.7500 x 2 = 1.5000
0.5000 x 2 = 1.0000 (l.s.b)

Decimal to Octal

Read
up

(167.6875)10 = ( ? )8 = (247.54)8
Integer part:
167/8 = 20 remainder 7 (l.s.d)
20/8 = 2 remainder 4
2/8 = 0 remainder 2 (m.s.d)
Fraction part:

Read
down

0.6875 x 8 = 5.500 (m.s.d)


0.500 x 8 = 4.000 (l.s.d)
33

ELE 107 Dr. Derya Altunay

0.78125 x 16 = 12.5 (m.s.d) (12 = C)


0.5 x 16
= 8.0 (l.s.d)
ELE 107 Dr. Derya Altunay

Read
up

Read
down

ELE 107 Dr. Derya Altunay

Decimal to Hexadecimal
(1631.78125)10 = ( ? )H = (65F.C8)H
Integer part:
1631/16 = 101 remainder 15 = F (l.s.d)
101/16 = 6 remainder 5
6/16 = 0 remainder 6 (m.s.d)
Fraction part:

32

ELE 107 Dr. Derya Altunay

34

Notes on Fractions
Radix point (number point)

Read
up

Decimal point in base 10


Binary point in base 2
Octal point in base 8
Hex point in base 16

Integer and fraction parts must be converted


separately

Read
down
35

ELE 107 Dr. Derya Altunay

36

ELE 107-Week 2: Number Systems & Binary Codes

Notes on Fractions

Notes on Fractions

Radix point: fixed reference for the conversion

Shift the radix point one place to the right

Digit to the left is a unit digit in every base


(Base)0 is always 1 regardless of the base

Effect: multiplies the number by the base number


Example: (427.6)10

(4276)10
Shift the radix point one place to the left

No exact relationship between fractional


numbers in different number bases

Effect: divides the number by the base number

Exact conversion may not be possible

Example: (427.6)10

37

ELE 107 Dr. Derya Altunay

(42.76)10

38

ELE 107 Dr. Derya Altunay

Notes on Fractions

Notes on Fractions
(0.10101)2 = (0.65625)10

(0.4293)10
Position

10-1

10-2

10-3

10-4

Value

1/10

1/100

1/1000

1/10000

Position

2-1

2-2

2-3

2-4

2-5

Value

1/2

1/4

1/8

1/16

1/32

Evaluate 4 x 1/10 2 x 1/100 9 x 1/1000 3 x1/1000

Evaluate 1 x 1/2 0 x 1/4 1x 1/8

Sum

Sum

.4

.02

.009

.0003

39

ELE 107 Dr. Derya Altunay

Notes on Fractions

0.125

ELE 107 Dr. Derya Altunay

0.03125

40

Notes on Fractions

(0.6875)10 = (0.1011)2

(0.9375)10 = (0.74)8

0.6875 x 2 = 1.3750

0.9375 x 8 = 7.5000

0.3750 x 2 = 0.7500

0.5000 x 8 = 4.0000

0.7500 x 2 = 1.5000

(terminates)

(0.513)10 = (?)8

0.5000 x 2 = 1.0000
(terminates)
ELE 107 Dr. Derya Altunay

.5

0 x 1/16 1 x 1/32

41

0.513 x 8 = 4.104
0.104 x 8 = 0.832
0.832 x 8 = 6.656
0.656 x 8 = 5.248
0.248 x 8 = 1.984
. . . (does not terminate!)
ELE 107 Dr. Derya Altunay

42

ELE 107-Week 2: Number Systems & Binary Codes

Notes on Fractions

Notes on Fractions

(0.975)10 = (?)8 = (0.76314)8

0.975 x 8 = 7.800
0.800 x 8 = 6.400
0.400 x 8 = 3.200
0.200 x 8 = 1.600
0.600 x 8 = 4.800
0.800 x 8 = 6.400

No general relationship between fractions of


types 1/10k and 1/2k
A number representable in decimal may not be
representable in binary
The converse is true: all fractions of the form 1/2k
can be represented in decimal

Fractional conversions from one base to


another are stopped
If there is a rational solution or
When the desired accuracy is attained

(repeates!)
43

ELE 107 Dr. Derya Altunay

ELE 107 Dr. Derya Altunay

Number Ranges

Data Representation
When we communicate with each other, the information
is represented in an understandable notation, e.g., we use

Assume a computer is processing 16-bit


unsigned integers and fractions:
Integer part (16-bit)

Digits to represent numbers


Letters to represent text

Fraction part (16-bit)

When we communicate with a computer:


Text and numbers are entered through keyboard/mouse
Text, numbers, and images are displayed on the screen

216-1

Integer range:
0 to
(0 to 65535)
16
Fraction range:
0 to (2 -1) / 216
(0 to 65535/65536 = 0.9999847412)

This may be referred to as an external representation.


How do humans store the information internally?
How do computers store the information internally?
45

ELE 107 Dr. Derya Altunay

ELE 107 Dr. Derya Altunay

Data Representation
Humans:

Computers:

The five types of data/information the


computer commonly manipulates:

Text, numbers,
sounds

???
Text, numbers,
images, sounds

Text, numbers,
images, sounds

Numeric
Character
Visual
Audio
Instructional

First, the data must be transformed (converted)


into an acceptable representation that the
computer will accept.

Binary Numbers
ELE 107 Dr. Derya Altunay

46

Data Representation

Internal

Text, numbers,
images, sounds

44

47

ELE 107 Dr. Derya Altunay

48

ELE 107-Week 2: Number Systems & Binary Codes

Binary Systems

Binary Systems

The system in which all computer data is


represented and manipulated is called the
binary system.
All modern computers work with a system of
numbers called binary numbers.

Binary circuits: Electronic devices are


cheapest and function most reliably if they
assume only two states.

Use only two symbols: 0 and 1.

Open circuit

Closed circuit
49

ELE 107 Dr. Derya Altunay

Binary Systems
Binary systems have two
values
Zero and One

Binary numbers are easy


to represent using
physical phenomena
All numbers can be
represented in binary as
well as in any other base

Zero
0
False
Off
Low V
0V
Current
N-S polar.
Cold

50

ELE 107 Dr. Derya Altunay

Binary Systems
One
1
True
On
High V
5V
No current
E-W polar.
Hot

ELE 107 Dr. Derya Altunay

51

A bit (binary digit)

ON

OFF

is the smallest unit of


information
can have two values
can represent numbers,
codes, or instructions

Think of these values as


switches: yes and no, black
and white, high and low, or
ON and OFF
52

ELE 107 Dr. Derya Altunay

Bits as Instructions

Off/On Switches

Larger chunks of information are stored by


grouping bits as units

The computer can represent data by


constructing combinations of off or on
switches.

8 bits (byte) = 256 different messages

The computer stores programs as collections


of bits.
For instance, 01101010 might instruct the
computer to add two numbers.

OFF

or

ON

Other bit instructions might include where


to find numbers stored in memory or where
to store them.
ELE 107 Dr. Derya Altunay

53

ELE 107 Dr. Derya Altunay

54

ELE 107-Week 2: Number Systems & Binary Codes

Zero or One?

The Bit

The binary system can also be represented by


the digits zero and one.

or

one bit
two bits

three bits

Zero (off) and one (on) make up the two


digits in the binary system.
55

ELE 107 Dr. Derya Altunay

Each 0 or 1
in the binary
system is
called a bit.

56

ELE 107 Dr. Derya Altunay

Representation of Numbers

The Byte
A group of 8 bits is called a byte.

The three-light system:


Has eight possible
combinations on and off.
Could be used to indicate the
numbers 0, 1, 2, 3, 4, 5, 6, 7.
0 = 000 4 = 100
1 = 001 5 = 101
2 = 010 6 = 110
3 = 011 7 = 111

= J

Storing Bytes

ELE 107 Dr. Derya Altunay

58

Computer Word

Storage and memory capacity is expressed


in the number of bytes they can hold:

A computer word is defined as the number of


bits that constitute a common unit of data.
Word length varies by computer.
For example:

1 kilobyte (KB) = 210 or 1024 bytes (1K)


1 megabyte (MB) = 220 or 1,048,576 bytes
1 gigabyte (GB) = 230 or 1,073,741,824 bytes
1 terabyte (TB) = 240 or about one million MB
1 petabyte (PB) = 250 or about one million GB

ELE 107 Dr. Derya Altunay

Each byte represents one character of data


(a letter, digit, or special character).

57

ELE 107 Dr. Derya Altunay

8 bits = 1 byte = one word length


32 bits = 4 bytes = one word length
64 bits = 8 bytes = one word length

Different computers use different numbers of


bits: Intel Pentium 32 bits , Intel Itanium 64 bits
59

ELE 107 Dr. Derya Altunay

60

10

ELE 107-Week 2: Number Systems & Binary Codes

Coding the Computer

Bits as Codes

A code for determining which group of bits


represent which characters on a keyboard is
called ASCII.
ASCII has been adopted, as the standard, by the
U.S. government and is found in a variety of
computers.
0 1 0 0 1 0 1 0
ASCII-8 code

ASCII - American Standard


Code for Information Interchange
Most widely used code,
represents each character as a
unique 8-bit code.

Keyboard character

ELE 107 Dr. Derya Altunay

61

62

ELE 107 Dr. Derya Altunay

Binary Codes for Decimals


The binary number system is the most natural
system for the computer, but people are
accustomed to the decimal system.
Computers work internally with binary
numbers, the I/O generally uses decimal
numbers (human-interface).

Binary Codes
ELE 107
Computers and Programming I

ELE 107 Dr. Derya Altunay

63

Binary Codes for Decimals

Terminology
A codeword (encoding) is a symbolic
representation based on the binary
alphabet, that is a sequence of 1s and 0s.

So, convert decimal numbers to binary,


perform operations in binary, and convert
the binary results back to decimal.
Therefore, we have to store the decimal
numbers in the computer in a way that they
can be converted to binary.
We represent the decimal digits by a
binary code.
ELE 107 Dr. Derya Altunay

64

ELE 107 Dr. Derya Altunay

The length of the codeword is the number


of bits in the codeword. For example,
0110010 has a length of seven.

65

ELE 107 Dr. Derya Altunay

66

11

ELE 107-Week 2: Number Systems & Binary Codes

Terminology

Terminology
An n-bit binary code is a group of n bits that
assume up to 2n distinct combinations of 1s
and 0s.
For example, a set of 4 distinct numbers can
be represented by 2-bit codes such that each
number in the set is assigned exactly one of
the combinations in {00,01,10,11}.

A code is a set of codewords. For example,


the set {110, 001, 101, 011, 111 } is a code
that has five codewords.
Because each codeword has the same
length, this is a fixed-length code.
Variable-length codes have codewords
with different lengths.

67

ELE 107 Dr. Derya Altunay

Terminology

BCD

If m symbols are to be encoded then 2n m


must be satisfied.
A code that uses n-bit strings need not contain
2n valid codewords.
At least four bits are necessary to represent
the ten decimal digits (6 are unused) .
There are many different ways to choose ten
4-bit codewords.

The most popular and natural decimal code is


called binary-coded decimal (BCD).
BCD numbers are decimal numbers even
though they are represented in bits.
Each decimal digit is represented by a 4-bit
unsigned binary number.
Multiple digit decimal numbers are represented
by coding each of the digits.
69

ELE 107 Dr. Derya Altunay

70

ELE 107 Dr. Derya Altunay

BCD

BCD
Decimal Symbol
0
1
2
3
4
5
6
7
8
9

BCD is different from converting a decimal


number to binary.
For example (97)10 , when converted to binary,
is (1100001)2.
97 when represented in BCD is (1001 0111)BCD.
Although BCD requires more bits, it is easier to
understand and interpret.

ELE 107 Dr. Derya Altunay

68

ELE 107 Dr. Derya Altunay

71

ELE 107 Dr. Derya Altunay

BCD Digit
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
72

12

ELE 107-Week 2: Number Systems & Binary Codes

BCD
BCD is a weighted code because each
decimal digit can be obtained by assigning a
fixed weight to each codeword bit.
The weights are 8, 4, 2, and 1 8421 code
Other binary codes besides BCD exist to
represent decimal digits:
2421 code
Excess-3 (XS-3) code
Bi-quinary code
1-out-of-10 code, etc.
ELE 107 Dr. Derya Altunay

73

74

ELE 107 Dr. Derya Altunay

Other codes for decimals

Other codes for decimals

The 2421 code and the Excess-3 (XS-3) code


are both examples of self-complementing
codes.
Self-complementing property:The 9s
complement of each digit (9-d) is obtained by
complementing each bit in the codeword.
Self-complementing property is useful when
performing operations such as subtraction.

The 2421 code is a weighted code.


The bits are multiplied by indicated weights
and the sum gives the decimal digit.
The XS-3 code is a nonweighted code.
XS-3 code is obtained from the
corresponding BCD code added to 3.
XS-3 code is useful in decimal subtraction.

ELE 107 Dr. Derya Altunay

75

76

ELE 107 Dr. Derya Altunay

Other codes for decimals

Gray Code

The biquinary code uses seven bits for each


decimal digit.
The first two bits indicate whether the digit
is in the range 0-4 or 5-9.
The last five bits indicate which of the five
numbers in the selected range is
represented.
Any advantage using more than the minimum
number of bits in a code ?

Sometimes it is desirable to use codes in which


all successive codewords differ in only one bit
(cyclic codes).
An important cyclic code is the Gray code.
Continuous, or analog, information is converted
into digital form by means of an A/D converter.
The Gray code, is sometimes used for the
converted digital data.
Used in electromechanical applications of
digital systems, such as copy machines, brake
systems, machine tools, etc.

ELE 107 Dr. Derya Altunay

77

ELE 107 Dr. Derya Altunay

78

13

ELE 107-Week 2: Number Systems & Binary Codes

Gray Code

Gray Code

The Gray code is a code in which only one bit of a


codeword changes between each pair of successive
codewords.
This is useful when the analog signal is on the
border between two of the binary coded values.
For example, switching from 7 (0111) to 8 (1000)
requires four bits to be changed simultaneously. If
this does not happen, then various illegitimate
numbers could be momentarily generated during
the transition.
Using the Gray code keeps the value from being
misrepresented in such cases.

Gray code counters are


sometimes used to provide
the timing sequences that
control the operations in a
digital system.
The Gray code is also an
example of a reflected
code ; i.e., the n-bit code
can be obtained by reflecting
the (n -1)st-bit code.
79

ELE 107 Dr. Derya Altunay

ELE 107 Dr. Derya Altunay

Gray Code
Binary to Gray :

Data Types: Alphanumeric


Alphanumeric:

g n = bn
g i = bi bi +1 ;

0 i n 1

(1 0 0 1 1 0)2 = (1 1 0 1 0 1)GRAY
Gray to Binary :

80

Characters: a b X Y
Number digits: 0 1 8 9
Punctuation marks: : ; ? !
Special-purpose characters: # & $ %

Numeric characters vs. numbers

bn = g n

Both entered as ordinary characters


Computer converts into numbers for calculation

bi = g n g n 1 L g i

Examples: Variables declared as numbers by the


programmer

( 0 i n 1)

Treated as characters if processed as text


Examples: Phone numbers, ZIP codes

(1 0 1 1 0 1)GRAY = (1 1 0 1 1 0)2
ELE 107 Dr. Derya Altunay

81

Alphanumeric Codes

ELE 107 Dr. Derya Altunay

82

Representing Characters
ASCII : most widely used coding scheme
Unicode : developed for worldwide use

Arbitrary choice of bits to represent


characters
Standard (consistency): input and output device
must recognize same code
Value of binary number representing character
corresponds to placement in the alphabet

ASCII file often used as a synonym for text


file (Although a text file can be encoded in
ASCII code, or Unicode)

Facilitates sorting and searching

ELE 107 Dr. Derya Altunay

83

ELE 107 Dr. Derya Altunay

84

14

ELE 107-Week 2: Number Systems & Binary Codes

ASCII

ASCII

Developed by ANSI (American National


Standards Institute)
American Standard Code for Information
Interchange.
The standard alphanumeric binary code
Represents

ASCII is an example of a fixed-size code with


a code length of seven
Basic ASCII uses 7 bits to code 128 characters
Most computers manipulate the 7-bit ASCII
code as a byte with the m.s.b set to 0.
This extra bit is used for specific purposes:

Latin alphabet, Arabic numerals, standard


punctuation characters
Plus small set of accents and other European
special characters

An additional 128 8-bit characters with the m.s.b


set to 1.
As a parity bit (check for memory/communication
errors)
85

ELE 107 Dr. Derya Altunay

ASCII

ELE 107 Dr. Derya Altunay

ASCII

87

Control and Printable Characters

ELE 107 Dr. Derya Altunay

88

Control and Printable Characters

Two subsets in the ASCII character set: the


control characters and the printable characters.
The control characters include the first 32
characters and the last character (delete).
The remaining characters are the printable
characters.
The printable characters can be generated by
typing the appropriate keys on keyboards.
The control characters can be generated by
holding down the control key while typing a
single character.
ELE 107 Dr. Derya Altunay

86

ELE 107 Dr. Derya Altunay

89

Decimal
0 31

ASCII
Invisible (control characters)

32 47
48 - 57
58 64

Punctuation, mathematical operations


Characters 0 9
Comparators and other miscellaneous characters : ; ? @

65 - 90
91 96
97 122
123 127

Alphabetic (upper case A - Z)


More miscellaneous characters [ \ ] ^ _ '
Alphabetic (lower case a - z)
More miscellaneous characters { | } ~ DEL

ELE 107 Dr. Derya Altunay

90

15

ELE 107-Week 2: Number Systems & Binary Codes

Decoding

Unicode

Since ASCII is a fixed-length code, it is easy


to decode sequences of encoded characters.
Start breaking the sequence into subsequences
consisting of 7 bits.
Then match each subsequence to a character
using the ASCII table.
Decode the ASCII string :
10010001100101110110011011001101111

Most common 16-bit form represents 65,536 symbols.


ASCII Latin-I subset of Unicode

1001000 1100101 1101100 1101100 1101111

Allows software modifications for local languages


Unicode used by Web browsers, Java, most software.

Values 0 to 255 in Unicode table

Multilingual: defines codes for


Nearly every character-based alphabet
Large set of ideographs for Chinese, Japanese and Korean
Composite characters for vowels and syllabic clusters
required by some languages

ELE 107 Dr. Derya Altunay

91

ELE 107 Dr. Derya Altunay

Some characters in Unicode

92

Some characters in Unicode

U+2601 (alt-09729) CLOUD = cloudy weather


U+2602 (alt-09730) UMBRELLA = rainy weather
U+2603 (alt-09731) SNOWMAN = snowy weather

U+03C0 GREEK SMALL LETTER PI

U+262F (alt-09775) YIN YANG

U+042F CYRILLIC CAPITAL LETTER YA

U+263A (alt-09786) WHITE SMILING FACE

U+97F3 CJK UNIFIED IDEOGRAPH-97F3

U+263B (alt-09787) BLACK SMILING FACE

U+00E6 LATIN SMALL LETTER AE


U+221E INFINITY

ELE 107 Dr. Derya Altunay

93

U+266A (alt-09834) EIGHTH NOTE = quaver

U+266B (alt-09835) BEAMED EIGHTH NOTES = beamed quavers

ELE 107 Dr. Derya Altunay

94

First 256 Codes


for Unicode

Chess symbols in Unicode


U+2654 (alt-09812) WHITE CHESS KING
U+2655 (alt-09813) WHITE CHESS QUEEN
U+2656 (alt-09814) WHITE CHESS ROOK
U+2657 (alt-09815) WHITE CHESS BISHOP
U+2658 (alt-09816) WHITE CHESS KNIGHT
U+2659 (alt-09817) WHITE CHESS PAWN

ELE 107 Dr. Derya Altunay

95

ELE 107 Dr. Derya Altunay

96

16

ELE 107-Week 2: Number Systems & Binary Codes

Character Encodings

Error Detection/Correction

Characters are symbols that we use in written


communication.
Machine cannot provide a direct representation
for every character we use (There are many
symbols and new symbols are created).
Each machine provides a limited set of
characters called the character set of the
machine.
Now almost every manufacturer uses the
ASCII character set.

The storage or transmission of an encoded value can


incorrectly change the value.
We can use an error detection or an error correction
strategy to limit the scope of that change.
In error detection, the goal is to detect errors that
occur. The detected errors cannot be corrected, but
can prompt the data to be retransmitted.
The goal of error correction is to correct any errors
that occur during transmission.
Error correction is frequently called error masking
because errors are not visible to any part of the
system other than the receiver.
97

ELE 107 Dr. Derya Altunay

Bit Flip

Example
Suppose that you
Bit
send the ASCII
flipped
encoding for the
none
character S
0
(101 0011).
1
Show the
2
different values
3
that a receiver
4
will receive if a
5
single bit is
flipped during
6
transmission.

To simplify the presentation, only fixed-length


codes will be considered.
We only consider errors that change individual
bits.
We ignore the possibility of inserting or
deleting bits.
Changes to bits are frequently called bit flips
because the change flips the bit from 0 to 1 or
from 1 to 0.
ELE 107 Dr. Derya Altunay

99

Hamming Distance

Received
pattern
101 0011
101 0010
101 0001
101 0111
101 1011
100 0011
110 0011
001 0011

ELE 107 Dr. Derya Altunay

Received
symbol
S
R
Q
W
[
C
c
^S
100

Hamming Distance

The Hamming distance defines a distance between


two codewords, you count the number of bits in
the first codeword that need to be changed to result
in the second codeword.

What is the Hamming distance between


1011001 and 1011010?
To convert the first pattern to the second , we need to
change the first and the second bits in the first pattern.
The Hamming distance between the two patterns is 2.
ELE 107 Dr. Derya Altunay

98

ELE 107 Dr. Derya Altunay

101

We generalize the Hamming distance to a code by


considering the elements of the code in a pair-wise
fashion.
Given a fixed-length code, the Hamming distance for
the code is the minimum distance between any two
codewords in the code.
What is the Hamming distance of the following code?
{ 1010, 1101, 0110 }
dist (1010, 1101) = 3
dist (1010, 0110) = 2
The Hamming distance is 2.
dist (1101, 0110) = 3
ELE 107 Dr. Derya Altunay

102

17

ELE 107-Week 2: Number Systems & Binary Codes

Hamming Distance

Hamming Distance

The Hamming distance between:

The Hamming distance for the code reflects the


degree of redundancy in the code.
If a code has a Hamming distance of two,
changing a single bit in any of the codewords
will result in a bit pattern that is not in the code.
Thus, we can decide if an error introduced a
single bit flip.
A code with a minimum distance of 2c + d + 1
can correct up to c bit errors and detect up to
additional d bit errors.

10111011 and 10010011 is ?


10111011
10010011 it is 2
91738254 and 92337254 is ?
91738254
92337254 it is 3
COMPUTATION and CALCULATION is ?
COMPUTATION
CALCULATION it is 4
103

ELE 107 Dr. Derya Altunay

104

ELE 107 Dr. Derya Altunay

Parity

Parity Bit

Parity provides a simple way to ensure that the


Hamming distance of a code is at least two.
The parity of a binary pattern is determined by
counting the number of 1s in the pattern.
If the number of 1s is even, then the pattern has even
parity ; otherwise, the pattern has odd parity.
What is the parity of the pattern 1010111?

To ensure that every codeword has the same


parity, we add a bit called the parity bit, to each
codeword.
Given a codeword, we set the parity bit to 1 or 0
to make sure that the resulting bit pattern has the
desired parity.
Then we add a parity bit to the ASCII code, the
parity bit is the 8th bit in the codeword.

Because the pattern has five 1s, it has odd parity.


If every codeword in a code has the same parity, the
code must have a Hamming distance of at least two.
105

ELE 107 Dr. Derya Altunay

Parity Bit

Example
Symbol
0
1
2
3
A
d
#

The parity bit is extremely important when two


computers exchange information.
The sender and the receiver must agree on the
type of parity.
The simple parity scheme provides us with
enough information to detect a single error, but
not enough to correct it.

ELE 107 Dr. Derya Altunay

106

ELE 107 Dr. Derya Altunay

107

ASCII
011 0000
011 0001
011 0010
011 0011
100 0001
110 0100
010 0011

ELE 107 Dr. Derya Altunay

Even parity
0011 0000
1011 0001
1011 0010
0011 0011
0100 0001
1110 0100
1010 0011

Odd parity
1011 0000
0011 0001
0011 0010
1011 0011
1100 0001
0110 0100
0010 0011

108

18