Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q 3. Storage 1. How these numbers will be stored in Computers memory a) unsigned

ID: 3727595 • Letter: Q

Question

Q 3. Storage 1. How these numbers will be stored in Computers memory a) unsigned int 41; b) float j=41; c) signed int k =-41; d) unsigned int l=41; e) charm= 255; 2. How the following numbers will be stored as Floating Point representation, assuming 1-bit sign 8-bits exponent 23 bits Mantissa/Fraction/significand Biased Value =127 a) 0.1 (Decimal number) b) -0.00010011 (Binary Number) c) 0.75 (Decimal Number) Lets computer has stored following numbers in its memory as 32 bit float value acc to Q 3 Part 2 specification. What is this number in decimal? 3. a) 01000000001000000000000000000000 Q 4. Arithmetic: Add the following signed numbers using 2' complement and representing each number as 4 bits At end write when overflow occurs and how can it be detected by hardware or software?

Explanation / Answer

1.

a)

The storage size of signed int is -32,768 to 32,767 , that consists of 1 bit that holds the sign and other bits that hold numbers from 0–32,767 i.e. 32,768 numbers so on the positive side we have the max number as 32,767 (because 0 is also on the positive side) and on the negative side we have the max number as 32,768 (only the magnitude).

After understanding this, let’s see what happens when we use unsigned int for storage, the signed bit is also used for storing numbers so the length of the integer doubles i.e. 32768*2 = 65536 and -1 for storing 0 so we have integer ranging from 0 to 65535.

That is how int datatype works in C.

The datatypes in C are static in nature, i.e. each datatype has a specific memory size allocated to it and each and every time you initialize them they require that much memory from RAM to store the value they currently hold.

For example, this code initializes three integers, gives them smaller values at first and larger values later.

int a=8;

int b=9;

int c=5;

a=12323;

b=66;

c=32668;

So if the size of int was kept to 5 bits as it initially stored a single digit only it could not store larger numbers that are added later.

Also, RAM is randomly accessed, so after giving ‘a’ 5 bits (for storing 8), some other program might load some other data next to the int, so when we request a larger space to store 12323, it will be granted somewhere else on the RAM, which will make it quite difficult to handle and make the code slow.

1.d)

4.

When adding two numbers, overflow occurs when the two operands have the same

leftmost bit and the leftmost bit of the answer is different.

When the operands have differing leftmost bits, overflow cannot occur when adding them together because we are adding a positive number with a negative number which means we are actually subtracting. It implies that the result cannot be bigger than the operands.

The leftmost bit is frequently referred to as the Most Significant Bit (MSB for short).

2.

a)

A number in Scientific Notation with no leading 0s is called a Normalised Number: 1.0 × 10-8

Not in normalised form: 0.1 × 10-7 or 10.0 × 10-9

Can also represent binary numbers in scientific notation: 1.0 × 2-3

Computer arithmetic that supports such numbers is called Floating Point.

The form is 1.xxxx… × 2yy…

Using normalised scientific notation

Simplifies the exchange of data that includes floating-point numbers

Simplifies the arithmetic algorithms to know that the numbers will always be in this form

Increases the accuracy of the numbers that can be stored in a word, since each unnecessary leading 0 is replaced by another significant digit to the right of the decimal point

Representation of Floating-Point numbers

-1S × M × 2E

Bit No

Size

Field Name

31

1 bit

Sign (S)

23-30

8 bits

Exponent (E)

0-22

23 bits

Mantissa (M)

A Single-Precision floating-point number occupies 32-bits, so there is a compromise between the size of the mantissa and the size of the exponent.

These chosen sizes provide a range of approx:

± 10-38 ... 1038

Overflow

The exponent is too large to be represented in the Exponent field

Underflow

The number is too small to be represented in the Exponent field

To reduce the chances of underflow/overflow, can use 64-bit Double-Precision arithmetic

Bit No

Size

Field Name

63

1 bit

Sign (S)

52-62

11 bits

Exponent (E)

0-51

52 bits

Mantissa (M)

providing a range of approx

± 10-308 ... 10308

These formats are called ...

IEEE 754 Floating-Point Standard

Since the mantissa is always 1.xxxxxxxxx in the normalised form, no need to represent the leading 1. So, effectively:

· Single Precision: mantissa ===> 1 bit + 23 bits

· Double Precision: mantissa ===> 1 bit + 52 bits

Since zero (0.0) has no leading 1, to distinguish it from others, it is given the reserved bitpattern all 0s for the exponent so that hardware won't attach a leading 1 to it. Thus:

Zero (0.0) = 0000...0000

Other numbers = -1S × (1 + Mantissa) × 2E

If we number the mantissa bits from left to right m1, m2, m3, ...

mantissa = m1 × 2-1 + m2 × 2-2 + m3 × 2-3 + ....

Negative exponents could pose a problem in comparisons.

For example (with two's complement):

Sign

Exponent

Mantissa

1.0 × 2-1

0

11111111

0000000 00000000 00000000

1.0 × 2+1

0

00000001

0000000 00000000 00000000

With this representation, the first exponent shows a "larger" binary number, making direct comparison more difficult.

To avoid this, Biased Notation is used for exponents.

If the real exponent of a number is X then it is represented as (X + bias)

IEEE single-precision uses a bias of 127. Therefore, an exponent of

-1 is represented as -1 + 127 = 126 = 011111102

0 is represented as 0 + 127 = 127 = 011111112

+1 is represented as +1 + 127 = 128 = 100000002

+5 is represented as +5 + 127 = 132 = 100001002

So the actual exponent is found by subtracting the bias from the stored exponent. Therefore, given S, E, and M fields, an IEEE floating-point number has the value:

-1S × (1.0 + 0.M) × 2E-bias

(Remember: it is (1.0 + 0.M) because, with normalised form, only the fractional part of the mantissa needs to be stored)

3.

2.5=00000001 000000001000000000000000000000

2.5=10.1b

In normalized form this is1.01 * 21

Mantissa:M-01000000000000000000000

23 bits without leading 1

Exponent E = 1+127=128=10000000b

Sign:s=0 (value stored is positive)

2.5=01000000001000000000000000000000

Bit No

Size

Field Name

31

1 bit

Sign (S)

23-30

8 bits

Exponent (E)

0-22

23 bits

Mantissa (M)