Float
numbers are stored in exponential form i.e.
(Mantissa)*10^ (Exponent)
Here
* indicates multiplication and ^ indicates power.
In
memory only Mantissa and Exponent is stored not *, 10 and ^.
Total
size of float data type: 32 bit
Those
bits are used in following manner:
Exponent
bit: 8
Mantissa
bit: 24
Mantissa
is signed number, so 24 bit are used as:
Mantissa_sign
bit: 1
Mantisaa_data
bit: 23
For
only mantissa:
Mantissa_sign
bit will zero if number is positive and Mantissa_sign bit will one if number is
negative.
Exponent
is also signed number, So 8 bit are used as:
Exponent_sign
bit: 1
Exponent_data
bit: 7
Following
figure illustrate how floating point number is stored in memory.
Five important rules:
Rule 1: To
find the mantissa and exponent, we convert data into scientific form.
Rule 2: Before
the storing of exponent, 127 is added to exponent.
Rule 3: Exponent
is stored in memory in first byte from right to left side.
Rule 4: If
exponent will negative number it will be stored in 2’s complement form.
Rule 5: Mantissa
is stored in the memory in second byte onward from right to left side. 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: First
digit i.e. 1, decimal point symbol, base of power i.e. 10, power symbol ^ and
multiplication symbol * are not stored in the memory.
Step3: find exponent and
mantissa and signed bit
Mantissa_data bit in binary = 0100100 11001100 11001101 (Only first 23 bit from left side)
Mantissa_sign bit: 1 (Since it is a negative number)
Exponent in decimal: 3
Question:
Why we
have taken right most bit of mantissa_data bit one instead of zero?
Step 5: Add 127 in the exponent and convert
in the binary number form.
(Why 127?
since size of exponent_data bit is 7 and maximum possible number in seven bit
will 1111111 in binary or 127 in decimal)
Exponent= 127+3=130
Binary value of 130 in eight bit: 1000001 0
Exponent_data bit: 1000001 (Take first seven bit from left side)
Exponent_sign bit: 0 (Take
rightmost bit)
Step 6: Now store the
Mantissa_data bit, Mantissa_sign bit, Exponent_data bit and Exponent_sign bit
at appropriate location as shown in the following figure.
Note: Mantissa_data bits are stored
from left to right while 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 bit: 11001101
Binary value of -52 in
eight bit: 11001100
Binary value of 36 in
eight bit: 00100100
Binary value of -63 in
eight bit: 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