0% found this document useful (0 votes)
28 views20 pages

Lcture 2 - Number-Systems

The document discusses number systems and their importance in computer programming, particularly focusing on binary, decimal, octal, and hexadecimal systems. It explains how numbers are represented in these systems, the concept of bits and bytes, and the methods for converting between different number bases. Additionally, it covers the mathematical principles underlying these conversions and the significance of positional notation.

Uploaded by

mazen45619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views20 pages

Lcture 2 - Number-Systems

The document discusses number systems and their importance in computer programming, particularly focusing on binary, decimal, octal, and hexadecimal systems. It explains how numbers are represented in these systems, the concept of bits and bytes, and the methods for converting between different number bases. Additionally, it covers the mathematical principles underlying these conversions and the significance of positional notation.

Uploaded by

mazen45619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Computer Programming

CHAPTER 2
NUMBER SYSTEMS
2
INFORMATION REPRESENTATION (1/4)

 Numbers are important to computers


 Represent information precisely
 Can be processed

 Examples
 Represent yes or no: use 0 and 1
 Represent the 4 seasons: 0, 1, 2 and 3

Number Systems and Codes CS2100


3
INFORMATION REPRESENTATION (2/4)

 Bit (Binary digit)


 0 and 1
 Represent false and true in logic
 Represent the low and high states in electronic
devices
 Other units
 Byte: 8 bits
 Word: Multiples of bytes (eg: 1 byte, 2 bytes, 4 bytes, 8
bytes, etc.), depending on the architecture of the
computer system
CS2100
4
INFORMATION REPRESENTATION (3/4)

 N bits can represent up to 2N values.

 Examples:
2 bits  represent up to 4 values (00, 01, 10, 11)
3 bits  rep. up to 8 values (000, 001, 010, …, 110, 111)
4 bits  rep. up to 16 values (0000, 0001, 0010, …., 1111)

CS2100
5
INFORMATION REPRESENTATION (4/4)

 To represent M values, log2M bits are required.


 Examples:
 32 values  requires 5 bits
 64 values  requires 6 bits
 1024 values  requires 10 bits
 40 values  how many bits?
 100 values  how many bits?

CS2100
6
DECIMAL (BASE 10) SYSTEM (1/3)

 A weighted-positional number system


 Base or radix is 10 (the base or radix of a number system is the
total number of symbols/digits allowed in the system)
 Symbols/digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
 Position is important, as the value of each symbol/digit is
dependent on its type and its position in the number
 Example, the 9 in the two numbers below has different values:
 (7594)10 = (7 × 103) + (5 × 102) + (9 × 101) + (4 × 100)
 (912)10 = (9 × 102) + (1 × 101) + (2 × 100)

CS2100
7
DECIMAL (BASE 10) SYSTEM (2/3)

 In general,

(anan-1… a0 . f1f2 … fm)10 =


(an x 10n) + (an-1x10n-1) + … + (a0 x 100) +
(f1 x 10-1) + (f2 x 10-2) + … + (fm x 10-m)

CS2100
7
DECIMAL (BASE 10) SYSTEM (3/3)

 Weighing factors (or weights) are in powers of 10:


… 103 102 101 100 . 10-1 10-2 10-3 …
 To evaluate the decimal number 593.68, the digit in
each position is multiplied by the corresponding
weight:
5102 + 9101 + 3100 + 610-1 + 810-2
= (593.68)10

CS2100
9
OTHER NUMBER SYSTEMS

 Binary (base 2)
 Weights in powers of 2
 Binary digits (bits): 0, 1
 Octal (base 8)
 Weights in powers of 8
 Octal digits: 0, 1, 2, 3, 4, 5, 6, 7.
 Hexadecimal (base 16)
 Weights in powers of 16
 Hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
CS2100
7
Binary (BASE 2) SYSTEM

 Weighing factors (or weights) are in powers of 10:


… 23 22 21 20 . 2-1 2-2 2-3 …
 To evaluate the decimal number 593.68, the digit in
each position is multiplied by the corresponding
weight:
122 + 1 21 + 1 20 + 12-1 + 12-2
= (7.75)10

CS2100
7
OCTAL (BASE 8) SYSTEM

 Weighing factors (or weights) are in powers of 10:


… 83 82 81 80 . 8-1 8-2 8-3 …
 To evaluate the decimal number 523.64, the digit in
each position is multiplied by the corresponding
weight:
582 + 281 + 380 + 68-1 + 48-2
= (339.8125)10

CS2100
7
HEXADECIMAL (BASE 16) SYSTEM

 Weighing factors (or weights) are in powers of 10:


… 163 162 161 160 . 16-1 16-2 16-3 …
 To evaluate the decimal number 5A3.C4, the digit
in each position is multiplied by the corresponding
weight:
5162 + A(10)161 + 3160 + C(12)16-1 + 416-2
= (1443.765625)10

CS2100
13
BASE-R TO DECIMAL CONVERSION

 Easy!
 1101.1012 = 123 + 122 + 120 + 12-1 + 12-3

 572.68 = 5  82 + 7  81 + 2  80 + 6  8-1

 2A.816 = 2  161 + 10  160 + 8  16-1

 341.245 = 3  52 + 4  51 + 1  50 + 2  5-1 + 4  5-2


CS2100
14
DECIMAL TO BINARY CONVERSION

 Method 1
 Sum-of-Weights Method

 Method 2
 Repeated Division-by-2 Method (for whole
numbers)
 Repeated Multiplication-by-2 Method (for
fractions)

CS2100
15
SUM-OF-WEIGHTS METHOD

 Determine the set of binary weights whose


sum is equal to the decimal number
 (9)10 = 8 + 1 = 23 + 20 = (1001)2
 (18)10 = 16 + 2 = 24 + 21 = (10010)2
 (58)10 = 32 + 16 + 8 + 2 = 25 + 24 + 23 + 21 = (111010)2
 (0.625)10 = 0.5 + 0.125 = 2-1 + 2-3 = (0.101)2

CS2100
16
REPEATED DIVISION-BY-2
 To convert a whole number to binary, use
successive division by 2 until the quotient is 0. The
remainders form the answer, with the first
remainder as the least significant bit (LSB) and the
last as the most significant bit (MSB).
2 43
(43)10 = (101011)2
2 21 rem 1  LSB
2 10 rem 1
2 5 rem 0
2 2 rem 1
2 1 rem 0
0 rem 1  MSB CS2100
17
REPEATED MULTIPLICATION-BY-2
 To convert decimal fractions to binary, repeated
multiplication by 2 is used, until the fractional
product is 0 (or until the desired number of decimal
places). The carried digits, or carries, produce the
answer, with the first carry as the MSB, and the last
as the LSB.
Carry
(0.3125)10 = (.0101)2 0.31252=0.625 0 MSB
0.6252=1.25 1
0.252=0.50 0
0.52=1.00 1 LSB
CS2100
18
CONVERSION BETWEEN DECIMAL

 Base-R to decimal: multiply digits with their


corresponding weights.
 Decimal to binary (base 2)
 Whole numbers repeated division-by-2
 Fractions: repeated multiplication-by-2
 Decimal to base-R
 Whole numbers: repeated division-by-R
 Fractions: repeated multiplication-by-R

CS2100
19
BINARY TO OCTAL/HEXADECIMAL
CONVERSION

 Binary  Octal: partition in groups of 3


 (10 111 011 001 . 101 110)2 = (2731.56)8
 Octal  Binary: reverse
 (2731.56)8 = (10 111 011 001 . 101 110)2
 Binary  Hexadecimal: partition in groups of 4
 (101 1101 1001 . 1011 1000)2 = ( 5D9.B8)16
 Hexadecimal  Binary: reverse
 (5D9.B8)16 = (101 1101 1001 . 1011 1000)2
CS2100
20

END

Number Systems and Codes CS2100

You might also like