0% found this document useful (0 votes)
6 views59 pages

Understanding Number Systems in Computing

Uploaded by

21bcs008
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)
6 views59 pages

Understanding Number Systems in Computing

Uploaded by

21bcs008
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

Number System

• Number system is a method of representing numbers with the help


of a set of symbols and rules.
• Numbers can have different base values like binary (base-2), octal
(base-8), decimal (base 10) and hexadecimal (base 16).
• The base number represents the number of digits used in that
numbering system.

• Digits for binary are: 0 and 1.


• Digits for octal are: 0, 1, 2, 3, 4, 5, 6 and 7.
• Digits for Decimal are: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
• Digits for Hexadecimal are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Yogendra Kumar
How is Information Represented in a
Computer?
Binary Representation

• The value 1 represents an “on” bit and a 0 represents an


“off” bit.

• This notation is described as binary form.


• For example, below is a single byte of memory that contains the
letter `A' (ASCII code 65; binary 1000001).
01000001
• bit: A single binary digit (0 or 1).

•Nibble: A collec on of four bits (say, 0110).

•Byte: A collec on of eight bits (say, 01000111).

• Word:
• Depends on the computer.

• Typically 4 or 8 Bytes (that is, 32 or 64 bits).


ti
ti
Memory Representation for calculation
• 1 bit = 0 or 1
• 1 Byte (B) = 8 bits
• 1 Kilobyte (KB) = 2^10 = 1024 bytes
• 1 Megabyte (MB) = 2^20 = 1024KB
• 1 Gigabyte (GB) = 2^30 = 1024 MB = 1024 *1024 KB
• 1 Terabyte (TB) = 2^40 = 1024 GB = 1024 * 1024 *1024 KB
Decimal To Binary Conversion
Example :: 239
Example :: 239

2. 239
2 119 --- 1
2. 59 --- 1
2 29 — 1
2. 14 --- 1
2 7 --- 0
2. 3 --- 1
2 1 --- 1
2 0 --- 1

(239)10 = (11101111)2
Decimal-to-Binary Conversion
• It is possible to find binary representation of a decimal
number N by converting its integral part, and its
fractional part separately.

• Example illustrated with the decimal number N =


109.78125.
Cont.
• To convert NI = 109 to binary equivalent, divide NI and
each successive quotient by 2, noting the remainders,
as follows :
Cont.
• To convert NF = 0.78125 to its binary equivalent,
multiply NF and each successive fractional part by 2,
noting the integral part of the product, as follows :

NF = 0.78125 = 0.11001

• The zero fractional part indicates the end of the calculations.


Cont.
• N = 109.78125. The binary equivalent of N is simply the
sum of these two equivalents:

• N = NI . NF = 1101101.11001
Example :: 37.0625
Example :: 37.0625

(37)10 = (100101)2
(.0625)10 = (.0001)2

∴(37.0625)10 = (100101 . 0001)2


Binary To Decimal
Example
• To convert (110101) to its decimal equivalent, write
the appropriate place value over each bit and then add
up those powers of two which are weighted by 1.
Example
• To convert 101.11012 to its decimal equivalent.
Example
• To convert 101.1101 to its decimal equivalent.
Octal System
• Since 8 = 2^3 , each octal digit has a unique 3-bit
binary representation.
Converting Octal to Binary

435208 = 1000111010100002
•Take each octal digit and convert each digit to a binary
triplet.
•Keep leading zeros: if required

4 3 5 2 0
100 011
0 101 010
0 000
Converting Binary to Octal
start
100011001010012
•Take the binary number and from right to left, group all
placeholders in triplets.

•Add leading zeros, if necessary

•Convert each triplet to its single-digit octal equivalent.

010 001 100 101 001


Cont..

010 001 100 101 001


2 1 4 5 1

010 001 100 101 0012


0 = 214518

22
Decimal ↔ Octal Conversion
The Process: Successive Division
• Divide the decimal number by 8; the remainder is the LSB of the octal
number .
• If the quotation is zero, the conversion is complete. Otherwise repeat
step (a) using the quotation as the decimal number. The new remainder
is the next most significant bit of the octal number.

Example:
Convert the decimal number 9410 into its octal equivalent.

11
8 94 r = 6 ← LSB

1 ∴ 9410 = 1368
8 11 r =3

0
8 1 r = 1 ← MSB
Example: Dec → Octal
Example:
Convert the decimal number 18910 into its octal equivalent.
Example: Dec → Octal
Example:
Convert the decimal number 18910 into its octal equivalent.

Solution:

23
8 189 r =5 ← LSB

2 ∴ 18910 = 2758
8 23 r =7

0
8 2 r=2 ← MSB
Octal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the Octal Number by its corresponding bit-
weighting factor (i.e., Bit-0→80=1; Bit-1→81=8; Bit-2→82=64; etc.).
• Sum up all of the products in step (a) to get the decimal number.

Example:
Convert the octal number 1368 into its decimal equivalent.

64 8 1
82 81 80
1 3 6 ∴ 136 8 = 9410

64 + 24 + 6 = 9410
Example: Octal → Dec
Example:
Convert the octal number 1348 into its decimal equivalent.
Example: Octal → Dec
Example:
Convert the octal number 1348 into its decimal equivalent.

Solution:

82 81 80
∴ 1348 = 9210
1 3 4

64 + 24 + 4 = 9210
Hexadecimal Number System
• Since 16 = 2^4 , each Hexadecimal digit has a
unique 4-bit binary representation.
• 16 different symbols (radix = 16).
0 ! 0000 8 ! 1000
1 ! 0001 9 ! 1001
2 ! 0010 A ! 1010
3 ! 0011 B ! 1011
4 ! 0100 C ! 1100
5 ! 0101 D ! 1101
6 ! 0110 E ! 1110
7 ! 0111 F ! 1111
Binary-to-Hexadecimal Conversion

• For the integer part,


–Scan the binary number from right to left.
–Translate each group of four bits into the
corresponding hexadecimal digit.
•Add leading zeros if necessary.
• For the fractional part,
–Scan the binary number from left to right.
–Translate each group of four bits into the
corresponding hexadecimal digit.
•Add trailing zeros if necessary.
Example

1.(1011 0100 0011)2 = (B43)16


2.(10 1010 0001)2 = (2A1)16
3.(.1000 010)2 = (.84)16
4.(101 . 0101 111)2 = (5.5E)16
Hexadecimal-to-Binary Conversion

• Translate every hexadecimal digit into its


4-bit binary equivalent.
• Examples:
(3A5)16 = (0011 1010 0101)2
(12.3D)16 = (0001 0010 . 0011 1101)2
(1.8)16 = (0001 . 1000)2
Hexadecimal-Decimal Interconversion
• To convert 73 516 to its decimal equivalent, express the
number in expanded notation, change D to 13, and
then calculate using decimal arithmetic.
𝐷
Cont.
• Convert 39.B816 to its decimal equivalent as follows:
Decimal to Hexadecimal
• To convert the decimal number P = 9719 to its
hexadecimal equivalent, divide P, and each successive
quotient by the base b = 16, noting the remainders, as
follows :
Cont.
• To convert the decimal fraction Q = 0.78125 to its
hexadecimal equivalent, apply the integral-part
algorithm, with b = 16, as follows :
Counting . . . 2, 8, 10, 16

Number System, Base 2, 8, 10, 16


Decimal Binary Octal Hexadecimal
0 00000 0 0
1 00001 1 1
2 00010 2 2
3 00011 3 3
4 00100 4 4
5 00101 5 5
6 00110 6 6
7 00111 7 7
8 01000 10 8
9 01001 11 9
10 01010 12 A
11 01011 13 B
12 01100 14 C
13 01101 15 D
14 01110 16 E
15 01111 17 F
16 10000 20 10
17 10001 21 11
18 10010 22 12
19 10011 23 13
Arithmetic in Binary Number System
• Arithmetic operations in number systems are usually
done in binary because designing of logic networks is
much easier than decimal.
Binary Arithmetic Operations
• Binary Addition

• There are four steps in binary addition, they are written below

•0+0=0
•0+1=1
•1+0=1
• 1 + 1 = 0 (carry 1 to the next significant bit)
Binary addition: example

1 1 (Carries)
1 1 0 1
+ 1 1 0 0
1 1 0 0 1 (Sum)

most significant least significant


bit (MSB) bit (LSB)
Cont.
• An example will help us to understand the addition
process. Let us take two binary numbers 10001001 and
10010101
Binary addition: Another example

1 0 0 1
+ 1 1 1 0
Binary addition: Another example

1 (Carries)
1 0 0 1
+ 1 1 1 0
1 0 1 1 1 (Sum)

most significant least significant


bit (MSB) bit (LSB)
Unsigned Binary Numbers
• An n-bit binary number
B = bn-1bn-2 …. b2b1b0
• 2n distinct combinations are possible, 0 to 2n-1.
• For example, for n = 3, there are 8 distinct
combinations.
– 000, 001, 010, 011, 100, 101, 110, 111
• Range of numbers that can be represented
n=8 ➔ 0 to 28-1 (255)
n=16 ➔ 0 to 216-1 (65535)
n=32 ➔ 0 to 232-1 (4294967295)
Signed Integer Representation
• Many of the numerical data items that are used
in a program are signed (positive or negative).
– Question:: How to represent sign?
• Three possible approaches:
– Sign-magnitude representation
– One’s complement representation
– Two’s complement representation
Sign-magnitude Representation
• For an n-bit number representation
– The most significant bit (MSB) indicates sign
0 ! positive
1 ! negative
– The remaining n-1 bits represent magnitude.
bn-1 bn-2 b1 b0

Sign
Magnitude
Contd.
• Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: − (2n-1 – 1)
• A problem:
Two different representations of zero.
+0 ! 0 000….0
-0 ! 1 000….0
One’s Complement Representation
• Basic idea:
– Positive numbers are represented exactly as in sign-
magnitude form.
– Negative numbers are represented in 1’s complement form.
• How to compute the 1’s complement of a number?
– Complement every bit of the number (1!0 and 0!1).
– MSB will indicate the sign of the number.
0 ! positive
1 ! negative
Example :: n=4

0000 ! +0 1111 ! -0
0001 ! +1 1110 ! -1
0010 ! +2 1101 ! -2
0011 ! +3 1100 ! -3
0100 ! +4 1011 ! -4
0101 ! +5 1010 ! -5
0110 ! +6 1001 ! -6
0111 ! +7 1000 ! -7

To find the representation of, say, -4, first note that


+4 = 0100
-4 = 1’s complement of 0100 = 1011
Two’s Complement Representation
• Basic idea:
– Positive numbers are represented exactly as in sign-
magnitude form.
– Negative numbers are represented in 2’s complement form.
• How to compute the 2’s complement of a number?
– Complement every bit of the number (1!0 and 0!1), and
then add one to the resulting number.
– MSB will indicate the sign of the number.
0 ! positive
1 ! negative
Example :: n=4

0000 ! +0 0000 ! -0
0001 ! +1 1111 ! -1
0010 ! +2 1110 ! -2
0011 ! +3 1101 ! -3
0100 ! +4 1100 ! -4
0101 ! +5 1011 ! -5
0110 ! +6 1010 ! -6
0111 ! +7 1001 ! -7
To find the representation of, say, -4, first note that
+4 = 0100
-4 = 2’s complement of 0100 = 1011+1 = 1100
Contd.
• Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: − (2n-1 – 1)
• A problem:
Two different representations of zero.
+0 ! 0 000….0
-0 ! 1 111….1

• Advantage of 1’s complement representation


– Subtraction can be done using addition.
– Leads to substantial saving in circuitry.
Contd.
• Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: − 2n-1
• Advantage:
– Unique representation of zero.
– Subtraction can be done using addition.
– Leads to substantial saving in circuitry.
• Almost all computers today use the 2’s complement
representation for storing negative numbers.
Subtraction Using Addition :: 1’s Complement

• How to compute A – B ?
– Compute the 1’s complement of B (say, B1).
– Compute R = A + B1
– If the carry obtained after addition is ‘1’
• Add the carry back to R (called end-around carry).
• That is, R = R + 1.
• The result is a positive number.
Else
• The result is negative, and is in 1’s complement form.
Example 1 :: 6 – 2

1’s complement of 2 = 1101


Assume 4-bit representations.
6 :: 0110 A
Since there is a carry, it is
-2 :: 1101 B1 added back to the result.

1 0011 R The result is positive.


End-around
carry 1
0100 ➔ +4
Example 2 :: 3 – 5

1’s complement of 5 = 1010

3 :: 0011 A
Assume 4-bit representations.
-5 :: 1010 B1
Since there is no carry, the result is
1101 R negative.
1101 is the 1’s complement of 0010,
-2
that is, it represents –2.
Subtraction Using Addition :: 2’s Complement

• How to compute A – B ?

– Compute the 2’s complement of B (say, B2).

– Compute R = A + B2

– Ignore carry if it is there.

– The result is in 2’s complement form.


Example 1 :: 6 – 2

2’s complement of 2 = 1101 + 1 = 1110

6 :: 0110 A

B2
-2 :: 1110
1 0100 R

Ignore carry +4
Example 2 :: 3 – 5

2’s complement of 5 = 1010 + 1 = 1011

A
3 :: 0011
B2
-5 :: 1011
1110 R

-2

You might also like