Lecture_CSP05

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Number Systems

How do we represent data in a computer?


 At the lowest level, a computer is an electronic machine.
 Works by controlling the flow of electrons
Computer is a binary digital system
Digital system:
Binary (base two) system:
Finite number of symbols
Has two states: 0 and 1

• Basic unit of information is the binary digit, or bit.


• Values with more than two states require multiple bits.
– A sequence of two bits has four possible states:
00, 01, 10, 11
– A sequence of three bits has eight possible states:
000, 001, 010, 011, 100, 101, 110, 111
• illation :
– A sequence of n bits has 2n possible states.
What kinds of data do we need to represent?
• Numbers – signed, unsigned, integers, floating point,
complex, rational, irrational, …
• Text – characters, strings, …
• Images – pixels, colors, shapes, …
• Sound
• Logical – true, false
• Instructions
• …… so on…
Number conversions
• Base conversion
– Convert a number from one number system to another
• Decimal to Binary
• Binary to Decimal
• Decimal to Octal
• Octal to Decimal
• Binary to Octal
• Octal to Binary
• Decimal to hexadecimal
• Hexadecimal to Decimal
• Binary to Hexadecimal
• Hexadecimal to Binary

4
What is Base?
• Decimal Number System
– Base is 10
– All numbers are represented by 0 to 9
• Binary Number System
– Base is 2
– All numbers are represented by 0 and 1
• Hexadecimal Number System
– Base is 16
– All numbers are represented by 0 to 15

• Inference
– Maximum digit value in any number system is Base -1
5
Decimal to Binary conversion
Decimal to Binary conversion
• Divide the number by 2 and take only the remainder, if division is
completed than take only the remainder which gives the binary number.
• the number is taken and counted from the last remainder

Example: converting the decimal number (87)10 into binary number

(87)10 = (1010111)2
Fraction Decimal Numbers to Binary
• For this the successive multiplication is done.
• The number which is to be converted is multiplied with base or radix
of binary number which is 2.
• The integer part or the carry of the product is taken out and the
same process is repeated until we get an integer.

Example: binary equivalent of (.95)10 is evaluated as

• we are not getting the integer value after successive multiplication,


• Thus we can approximate the value to be (.111110….)
Binary to Decimal conversion
• Have weight of the binary digits from most significant bit
• Now the bits are multiplied with their weights, and the sum of
those products is the respective decimal number.
Example

Convert the binary number (1110)2 to a decimal number


Binary to Decimal conversion
• Have weight of the binary digits from most significant bit
• Now the bits are multiplied with their weights, and the sum of
those products is the respective decimal number.
Example

Convert the binary number (1110)2 to a decimal number


Fractions: Fixed-Point
• How can we represent fractions?
– Use a “binary point” to separate positive from
negative powers of two -- just like “decimal
point.”

2-1 = 0.5
2-2 = 0.25
Example: 2-3 = 0.125

11111110.110

11
Conversion from Binary to Decimal (Fractions)

Convert to decimal: 00101000.101

Step1: Take integer part and convert to decimal


25 + 23 = 40
Step2: Take fraction part and convert to decimal
2-1+2-3 = .625
Step3: Add result of Step1 and Step2
40.625

12
Conversion of Decimal Point Number to Decimal

• After the decimal point the number should be multiplied with


2-1, 2-2 etc.

Example:
Binary Numbering System
• Decimal number is a weighted number, converting from decimal to binary
(base 10 to base 2) will also produce a weighted binary number with the
right-hand most bit being the Least Significant Bit(LSB), and the left-
hand most bit being the Most Significant Bit(MSB), as shown below:

MSB Binary Digit LSB

28 27 26 25 24 23 22 21 20

256 128 64 32 16 8 4 2 1

• Conversion of a Binary to Decimal number as

Decimal Digit Value 256 128 64 32 16 8 4 2 1

Binary Digit Value 1 0 1 1 0 0 1 0 1


Octal Number System
Decimal to Octal Conversion
Octal to Decimal Conversion
Octal to Decimal conversion
Octal to Binary
Hexadecimal Notation
• It is often convenient to write binary (base-2) numbers
as hexadecimal (base-16) numbers instead.
• Why?
– Fewer digits -- four bits per hex digit
– Less error prone -- easy to corrupt long string of 1’s and 0’s

Binary Hex Decimal Binary Hex Decimal


0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15
Decimal to Hexadecimal

(423)10 =(1A7)16 (3479)10 =(D97)16

(15605605)10 =(EE1F65)16
Hexadecimal to Decimal
Converting from Binary to Hexadecimal
• Every four bits is a hex digit.
– Start grouping from right-hand side

Example: 011101010001111010011010111

3 A 8 F 4 D 7

24
Hexadecimal is not a new machine representation, just a
convenient way to write the number.
Hexadecimal to Binary
• first of all we have to convert every digit of the hexadecimal
number into its equivalent binary numbers (making every group a
group of four by adding 0s to the left of the number).
Problem
How we convert a Hexadecimal Number into Binary Number?
Attempt following:
i. ADF6
ii. DB69
iii. CA36
iv. 5678
v. DABC

Convert the above given number into octal and decimal number

29
Binary Number Names & Prefixes
classification of individual bits into larger groups are generally
referred to by the following more common names of:

Number of Binary Digits (bits) Common Name

1 Bit

4 Nibble

8 Byte

16 Word

32 Double Word

64 Quad Word
Number of Bytes Common Name

1,024 (210) kilobyte (kb)

1,048,576 (220) Megabyte (Mb)

1,073,741,824 (230) Gigabyte (Gb)

a very long number! (240) Terabyte (Tb)


Questions??

You might also like