Floating-point numbers are stored in exponential form, expressed as (Mantissa) * 10^(Exponent), where '' denotes multiplication and '^' represents power. In memory, only the Mantissa and Exponent are stored, excluding '', 10, and '^'.
For the float data type with a total size of 32 bits, the bits are allocated as follows:
- Exponent bits: 8
- Mantissa bits: 24
As the Mantissa is a signed number, the 24 bits are further divided into:
- Mantissa_sign bit: 1
- Mantissa_data bits: 23
For the mantissa alone:
The Mantissa_sign bit is set to zero if the number is positive and one if the number is negative.
Regarding the exponent, which is also a signed number:
Exponent_sign bit: 1
Exponent_data bits: 7
Following
figure illustrate how floating point number is stored in memory.
Certainly, you've summarized the rules effectively:
Rule 1: To determine the mantissa and exponent, convert data into scientific form.
Rule 2: Add 127 to the exponent before storing it.
Rule 3: Store the exponent in memory's first byte, moving from the right to the left side.
Rule 4: If the exponent is a negative number, store it in 2’s complement form.
Rule 5: Store the mantissa in memory, starting from the second byte and moving from right to left.
Example:
Memory representation of:
float a = -10.3f;
For
this you have to follow following steps:
step1: convert
the number (10.3) into binary form
Binary value of 10.3 is: 1010.0100110011001100110011001100110011…
step2: convert the above
binary number in the scientific form. Scientific form of
1010.0100110011001100110011001100110011…=
1.01001001100110011001100 11001100110011…*10^3
Note: The memory representation of floating-point numbers excludes the storage of the first digit (1), the decimal point symbol, the base of power (10), the power symbol (^), and the multiplication symbol (*).
Step 3: To find the exponent and mantissa along with the signed bit.
- Mantissa_data bit in binary: 0100100110011001101101 (considering only the first 23 bits from the left side).
Mantissa_sign bit: 1 (indicating a negative number)
Exponent in decimal: 3
Question:
Why we
have taken right most bit of mantissa_data bit one instead of zero?
Certainly, let's add 127 to the exponent and convert the result into binary:
Exponent in decimal+127=3+127=130
Now, in binary form:
13010=100000102
So, after adding 127 to the exponent, the binary representation is 100000102.
(The choice of 127 is related to the size of the exponent_data field, which is 7 bits. The maximum value that can be represented with 7 bits is 127 in decimal, or 1111111 in binary.
So, for an exponent of 3:
Exponent=127+3=130
In binary, 130 is represented as 10000010. The exponent_data bit is then derived by taking the first seven bits from the left side (1000001), and the exponent_sign bit is the rightmost bit (0).
Step 6: Storing the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit, and Exponent_sign bit at their respective locations.
- Mantissa_data bit: 0100100110011001101101 (only the first 23 bits from the left side)
- Mantissa_sign bit: 1 (indicating a negative number)
- Exponent_data bit: 1000001 (first seven bits from the left side)
- Exponent_sign bit: 0 (rightmost bit)
as shown in the following figure.
Note: Mantissa_data bits are stored from left to right, whereas Exponent_data bits are stored from right to left.
How to check above memory representation is correct?
Answer:
We
will take one char pointer and visit each byte of a float number and observe
the output.
#include<stdio.h>
int main(){
int i;
float f=-10.3f;
char *p=(char *)&f;
for(i=0;i<4;i++)
printf("%d
",*p++);
return 0;
}
Output: -51 -52 36 -63
Explanation:
- Binary value of -51 in eight bits: 11001101
- Binary value of -52 in eight bits: 11001100
- Binary value of 36 in eight bits: 00100100
- Binary value of -63 in eight bits: 11000001
This
is exactly same as which we have represented in memory in the above figure.
10 comments:
Question #4
I think it should be
printf("%f",*arr[1]);
1. Write a “C1GG filter” that reads a message entered by the user and translates
it into C1GG message:
Sample Run:
Enter message: Hey dude, C is rilly cool
C1GG message: H3Y DUD3, C 15 R1LLY C00L!!!!!
The program should convert the message to upper-case letters, substitute
digits for certain letters (A->4, B->8, I->1, O->0, S->5), and then append 5
exclamation marks.
2. Write a function that computes the value of the polynomial
5 4 3 2 3 2 5 7 6 x x x x x . Write a program that asks the user to enter a
value for x , calls the function to compute the value of the polynomial, and
display the value returned by the function.
3. Write a program that reads a message and checks whether it is a palindrome
or not. Use array name as a pointer to keep track of positions in the array
(Ignore special characters).
Sample Run:
Enter a message: Madam, I am Adam.
The message entered is not a palindrome.
4. Write a program that prompts the user to enter two dates and then find which
date comes earlier on the calendar. Use structure to store date and function
to compare dates.
Sample Run:
Enter first date (mm/dd/yy) : 3/8/10
Enter second date (mm/dd/yy) : 5/17/11
3/8/10 is earlier than 5/17/11.
5. (C99) Write a program that converts a number in Cartesian coordinates to
polar form. The user will enter two values (real and imaginary parts of the
number). The program will display the values of r and .
How can I get answers for those 5.plz anyone can help me.
#include
int factorial(int n)
{
int res=1;
int count;
for(count=1;count<=n;count++)
{
res=res*count;
}
printf("%d",res);
return res;
}
int main()
{
int f,res;
scanf("%d",&f);
res=factorial(f);
printf("%d",res);
}
plz tell me how can i get these question answers pls tell me
can anyone help me in this question plzz
The table below shows the normal boiling point of several substances.write a program that prompts the user for the observed boiling point of a substance in celsius and identifies the substance if the observed boiling point is within 5% of the expected boiling point.if the data input is more than 5% higher or lower than any of the boiling points in the table,the program should output the message substance unknown.
SUBSTANCE NORMAL BOILING POINT (c)
Water 100
Mercury 357
Copper 1187
Silver 2193
Gold 2660
cn i get the answer for the above ques plz............its urgent
Getting error for 6th question as"cannot cast from unsigned long to char"plz help
Dude its very simple , please let me know if you still need its answer at anandhosamani@gmail.com
write a c program to print number of two's in two table?????
Post a Comment