0% found this document useful (0 votes)
18 views

11 Number Systems

The document discusses number systems used in computers including binary, hexadecimal, and decimal. It explains how numeric data is stored and represented in memory as signed and unsigned integers using binary numbers. Binary to decimal and decimal to binary conversions are demonstrated along with hexadecimal representations.

Uploaded by

sesi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

11 Number Systems

The document discusses number systems used in computers including binary, hexadecimal, and decimal. It explains how numeric data is stored and represented in memory as signed and unsigned integers using binary numbers. Binary to decimal and decimal to binary conversions are demonstrated along with hexadecimal representations.

Uploaded by

sesi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Number Systems

CS10003 PROGRAMMING AND DATA STRUCTURES

1
Number Representation
BINARY
HEXADECIMAL
DECIMAL

2
Topics to be Discussed

How are numeric data items actually stored in computer memory?

How much space (memory locations) is allocated for each type of data?
• int, float, char, double, etc.

How are characters and strings stored in memory?


• Already discussed.

3
Number System: The Basics

We are accustomed to using the so-called decimal number system.


• Ten digits :: 0,1,2,3,4,5,6,7,8,9
• Every digit position has a weight which is a power of 10.
• Base or radix is 10.

Example:

234 = 2 x 102 + 3 x 101 + 4 x 100

250.67 = 2 x 102 + 5 x 101 + 0 x 100 + 6 x 10–1 + 7 x 10–2

4
Binary Number System

Two digits:
• 0 and 1.
• Every digit position has a weight which is a power of 2.
• Base or radix is 2.

Example:

110 = 1 x 22 + 1 x 21 + 0 x 20

101.01 = 1 x 22 + 0 x 21 + 1 x 20 + 0 x 2–1 + 1 x 2–2

5
Binary-to-Decimal Conversion
Each digit position of a binary number has a weight.
• Some power of 2.

A binary number:
B = bn–1 bn–2 … b1 b0 . b–1 b–2 … b–m

Corresponding value in decimal:


n–1
D= Σ bi 2i
i = –m

6
Examples
1. 101011 ⇒ 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 43
(101011)2 = (43)10

2. .0101 ⇒ 0 x 2–1 + 1 x 2–2 + 0 x 2–3 + 1 x 2–4 = .3125


(.0101)2 = (.3125)10

3. 101.11 ⇒ 1 x 22 + 0 x 21 + 1 x 20 + 1 x 2–1 + 1 x 2–2 = 5.75


(101.11)2 = (5.75)10

7
Decimal-to-Binary Conversion
Consider the integer and fractional parts separately.
For the integer part,
• Repeatedly divide the given number by 2, and go on accumulating the remainders, until
the number becomes zero.
• Arrange the remainders in reverse order.
For the fractional part,
• Repeatedly multiply the given fraction by 2.
• Accumulate the integer part (0 or 1).
• If the integer part is 1, chop it off.
• Arrange the integer parts in the order they are obtained.

8
Example 1 :: 239

2 239
2 119 --- 1
2 59 --- 1
2 29 --- 1
2 14 --- 1
(239)10 = (11101111)2
2 7 --- 0
2 3 --- 1
2 1 --- 1
2 0 --- 1

9
Example 2 :: 64

2 64
2 32 --- 0
2 16 --- 0
2 8 --- 0
2 4 --- 0
(64)10 = (1000000)2
2 2 --- 0
2 1 --- 0
2 0 --- 1

10
Example 3 :: .634

.634 x 2 = 1.268
.268 x 2 = 0.536
.536 x 2 = 1.072
(.634)10 = (.10100…)2
.072 x 2 = 0.144
.144 x 2 = 0.288
:
:

11
Example 4 :: 37.0625

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

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

12
Hexadecimal Number System

A compact way of representing binary numbers.


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

13
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.

14
Examples
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

15
Hexadecimal-to-Binary Conversion
Translate every hexadecimal digit into its 4-bit binary equivalent.
• Discard leading and trailing zeros if desired.

Examples:
(3A5)16 = (0011 1010 0101)2
(12.3D)16 = (0001 0010 . 0011 1101)2
(1.8)16 = (0001 . 1000)2

16
Representation of
Unsigned and Signed Integers

17
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)

18
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:


a) Sign-magnitude representation
b) One’s complement representation
c) Two’s complement representation

19
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

20
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

21
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

22
Example :: n = 4
0000 ⇒ +0 1000 ⇒ -7
0001 ⇒ +1 1001 ⇒ -6
0010 ⇒ +2 1010 ⇒ -5
0011 ⇒ +3 1011 ⇒ -4
0100 ⇒ +4 1100 ⇒ -3
0101 ⇒ +5 1101 ⇒ -2
0110 ⇒ +6 1110 ⇒ -1
0111 ⇒ +7 1111 ⇒ -0

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


+4 = 0100
−4 = 1’s complement of 0100 = 1011
23
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.
24
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

25
Example :: n = 4
0000 ⇒ +0 1000 ⇒ -8
0001 ⇒ +1 1001 ⇒ -7
0010 ⇒ +2 1010 ⇒ -6
0011 ⇒ +3 1011 ⇒ -5
0100 ⇒ +4 1100 ⇒ -4
0101 ⇒ +5 1101 ⇒ -3
0110 ⇒ +6 1110 ⇒ -2
0111 ⇒ +7 1111 ⇒ -1

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


+4 = 0100
−4 = 2’s complement of 0100 = 1011+1 = 1100
26
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.

27
Contd.
In C, typically:
• char
• 8 bits ⇒ + (27– 1) to –27
• short int
• 16 bits ⇒ + (215−1) to −215
• int
• 32 bits ⇒ + (231−1) to −231
• long int
• 64 bits ⇒ + (263−1) to −263

28
Binary operations

Addition / Subtraction using addition

29
Binary addition
Rules for adding two bits Rules for adding three bits
0+0 is 0
0+1 is 1 a b cin cout s
1+0 is 1 _____________________
1+1 is 10, that is, 0 with carry of 1 0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
_____________________
30
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.

31
Example 1 :: 6 – 2
1’s complement of 2 = 1101

6 :: 0110
–2 :: 1101 A
Assume 4-bit representations.
1 0011 B1 Since there is a carry, it is added back to the
1 R result.
0100 ⇒ +4 The result is positive.

End-around
carry

32
Example 2 :: 3 – 5
1’s complement of 5 = 1010

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

33
Subtraction Using Addition :: 2’s Complement
How to compute A – B ?
• Compute the 2’s complement of B (say, B2).
• Compute R = A + B2
• If the carry obtained after addition is ‘1’
• Ignore the carry.
• The result is a positive number.
Else
• The result is negative, and is in 2’s complement form.

34
Example 1 :: 6 – 2
2’s complement of 2 = 1101 + 1 = 1110

6 :: 0110
–2 :: 1110 A
1 0100 B2 Assume 4-bit representations.
Presence of carry indicates that the result is
R positive.
+4
No need to add the end-around carry like in 1’s
complement.
Ignore carry

35
Example 2 :: 3 – 5
2’s complement of 5 = 1010 + 1 = 1011

3 :: 0011
–5 :: 1011 A
1110 B2
R
-2 Assume 4-bit representations.
Since there is no carry, the result is negative.
1110 is the 2’s complement of 0010, that is, it
represents –2.

36
2’s complement arithmetic: More Examples
▪ Example 1: 18 – 11 = ?
▪ 18 is represented as 00010010
▪ 11 is represented as 00001011
• 1’s complement of 11 is 11110100
• 2’s complement of 11 is 11110101
▪ Add 18 to 2’s complement of 11

00010010
+ 11110101
---------------- 00000111 is 7
00000111 (with a carry of 1
which is ignored)
37
2’s complement arithmetic: More Examples
▪ Example 2: 7 – 9 = ?
▪ 7 is represented as 00000111
▪ 9 is represented as 00001001
• 1’s complement of 9 is 11110110
• 2’s complement of 9 is 11110111
▪ Add 7 to 2’s complement of 9

00000111
+ 11110111 11111110 is –2
----------------
11111110 (with a carry of 0
which is ignored) 38
Overflow and Underflow
Adding two +ve (-ve) numbers should not produce a –ve (+ve) number.
If it does, overflow (underflow) occurs

Another equivalent condition :


carry in and carry out from Most Significant Bit (MSB) differ.

(64) 01000000 (64) 01000000


( 4) 00000100 (96) 01100000
-------------- --------------
(68) 01000100 (-96) 10100000
carry (out)(in) carry (out)(in) differ:
0 0 0 1 overflow
39
Floating-point number representation
The IEEE 754 Format

40
Fixed Point Representation
 

41
Limitations of using Fixed Point Representation
 

Floating point numbers address this issue, and is made of fixed point
signed-magnitude number and an accompanying scale factor. 42
Normalization
Write a positive non-zero number as

1.b1b2b3…bk x 2E = (1 + b1 x 2–1 + b2 x 2–2 + b3 x 2–3 + … + bk x 2–k) x 2E

Examples

Original Number Move Normalized Representation

+1010001.1101 ←6 + 1.0100011101 x 26

–111.000011 ←2 – 1.11000011 x 22

+0.00000111001 6→ + 1.11001 x 2–6

–0.001110011 3→ – 1.110011 x 2–3 43


Normalized numbers in Single Precision Format
The normalized numbers are

(–1)s 1.f x 2 E – 127.

Here, s is the sign bit, f is the mantissa (fractional part), and E is the exponent (plus 127).
The 1 before the binary point is not stored.

44
IEEE standards for floating-point representation

45
Example

Show the representation of the normalized number + 1.01000111001 x 26.

Solution

The sign is positive. The Excess_127 representation of the exponent is 133.


You add extra 0s on the right to make it 23 bits. The number in memory is
stored as:

0 10000101 01000111001000000000000

46
Example of floating-point representation

Number Sign Exponent Mantissa

– 1.11000011 x 22 1 10000001 11000011000000000000000

+ 1.11001 x 2–6 0 01111001 11001000000000000000000

– 1.110011 x 2–3 1 01111100 11001100000000000000000

47
Example

Interpret the following 32-point floating-point number

1 01111100 11001100000000000000000

Solution

The sign is negative.


The exponent is 124 – 127 = –3
The number is
–1.110011 x 2–3 = – (1 + (½) + (½)2 + (½)5 + (½)6 ) x 2–3
= 1.796875 x 2–3 = 0.224609375.

48
Range of normalized numbers
 

normalized normalized
negative numbers underflow positive numbers
overflow overflow

49
Denormalized numbers
● These numbers correspond to the 8-bit exponent E = 0

● If M denotes the 23-bit mantissa, then the number is to be interpreted as:

(–1)S x 0.M x 2–126 = (–1)S x M x 2–149

● The largest positive denormalized number is 11111111111111111111111 x 2–149 = (223 – 1) x 2–149 =


2–126 – 2–149. This is slightly smaller than the smallest normalized number.

● For each decrement of M by 1, the value of the denormalized number reduces by 2–149. The smallest
positive denormalized number is 2–149 (corresponding to M = 00000000000000000000001).

● When all bits of M are zero, we get the representation of +0 as a string of 32 zero bits.

● –0 is represented as 1 followed by 31 zero bits.

● This process of going from 2–126 to 0 is called gradual underflow.


50
Special numbers

These numbers correspond to the 8-bit exponent E = 255 (all 1 bits).

0 11111111 00000000000000000000000 +Inf


1 11111111 00000000000000000000000 –Inf
0 11111111 Any non-zero value NaN
1 11111111 Any non-zero value NaN

Inf means Infinity.


NaN means Not a Number.

51
A program to view the floating-point representation
#include <stdio.h>
int main ()
void prn32 ( unsigned a ) {
{ float x = -123.45;
int i; unsigned *p;

for (i=31; i>=0; --i) { p = (unsigned *)&x;


printf("%d", (a & (1U << i)) ? 1 : 0 ); prn32(*p);
if ((i == 31) || (i == 23)) printf(" ");
} return 0;
printf("\n"); }
}

Output
1 10000101 11101101110011001100110
52
Check for correctness
● 123 = 64 + 32 + 16 + 8 + 2 + 1 = 26 + 25 + 24 + 23 + 21 + 20 = 1111011

● 0.45 x 2 = 0.90, 0.90 x 2 = 1.80, 0.80 x 2 = 1.60, 0.60 x 2 = 1.20, 0.20 x 2 = 0.40, 0.40 x 2 = 0.80, …

● 0.45 = 0.0111001100

● 123.45 = 1111011.0111001100 ≈ 1111011.01110011001100110


= 1.11101101110011001100110 x 26
= 1.11101101110011001100110 x 2133 – 127
= 1.11101101110011001100110 x 2(128 + 4 + 1) – 127
= 1.11101101110011001100110 x 210000101 – 127

● What we should have: 1 10000101 11101101110011001100110


● What the program gives: 1 10000101 11101101110011001100110
53

You might also like