C Programming - Floating Point Issues - Discussion
Discussion Forum : Floating Point Issues - General Questions (Q.No. 3)
3.
If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program (on intel machine)?
#include<stdio.h>
#include<math.h>
int main()
{
float a=5.375;
char *p;
int i;
p = (char*)&a;
for(i=0; i<=3; i++)
printf("%02x\n", (unsigned char)p[i]);
return 0;
}
Discussion:
117 comments Page 2 of 12.
Er.Nitesh mathur said:
1 decade ago
Preethi is wrong because. Defination is given by preethi is totally wrong.
" Big ENDIAN" byte order-> the higher order byte of number is stored in lowest address. The result is written from top to bottom.
" LITTLE ENDIAN"-> byte order, the higher order byte of number is stored in higher address. The result is written from bottom to top.
" Big ENDIAN" byte order-> the higher order byte of number is stored in lowest address. The result is written from top to bottom.
" LITTLE ENDIAN"-> byte order, the higher order byte of number is stored in higher address. The result is written from bottom to top.
Avanthika said:
5 years ago
Binary equivalent of 5.375 in normalized form is;
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (Intel processors) use " LITTLE ENDIAN" byte order, the higher-order0 byte of the number is stored in lowest address. The result is written from bottom to top.
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (Intel processors) use " LITTLE ENDIAN" byte order, the higher-order0 byte of the number is stored in lowest address. The result is written from bottom to top.
(10)
Preethi said:
2 decades ago
Binary equivalent of 5.375 in normalised form is
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.
(1)
Bhaskar said:
1 decade ago
Binary equivalent of 5.375 in normalised form is
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.
0100 -> 4
0000 -> 0
1010 -> A
1100 -> C
0000 -> 0
0000 -> 0
0000 -> 0
0000 -> 0
Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.
Nikita said:
1 decade ago
a=5.375 IS REPRESENTED AS '40 AC 00 00'. When stored in memory in case of little endian its stored as..
(eg.) 500 501 502 503
a = 00 00 AC 40
Now p=(char *)&a; means p will hold address of a and p is of char type so after typecasting it will hold 500 which hold 00, on incrementing it will print respective values.
(eg.) 500 501 502 503
a = 00 00 AC 40
Now p=(char *)&a; means p will hold address of a and p is of char type so after typecasting it will hold 500 which hold 00, on incrementing it will print respective values.
Shubham Gorde said:
10 years ago
For little Indian method, least significant bytes stored first (means at lowest address).
Address value.
503 0100 0000.
502 1010 1100.
501 0000 0000.
500 0000 0000.
And statement p = (char *) &a:: stores low byte address (i.e 500 here) of 1 into p (as p is char pointer).
So answer is 00 00 AC 40.
Address value.
503 0100 0000.
502 1010 1100.
501 0000 0000.
500 0000 0000.
And statement p = (char *) &a:: stores low byte address (i.e 500 here) of 1 into p (as p is char pointer).
So answer is 00 00 AC 40.
Ravi jivani said:
1 decade ago
Its because the memory allocation type which intel CPU supports is little endian, so the last byte of the data stores first in the memory allocation unit. And the starting point of the pointer goes to that address. Its simple, just reverse the byte order of the binary equivalent data of the input value.
Santosh said:
1 decade ago
#include<stdio.h>
#include<math.h>
int main()
{
float a=5.375;
char *p;
int i;
p = (char*)&a;
for(i=0; i<=3; i++)
printf("%02x\n", (unsigned char)p[i]);
return 0;
}
Please give me the description for how the above program is executing.
#include<math.h>
int main()
{
float a=5.375;
char *p;
int i;
p = (char*)&a;
for(i=0; i<=3; i++)
printf("%02x\n", (unsigned char)p[i]);
return 0;
}
Please give me the description for how the above program is executing.
Dhirendra said:
1 decade ago
@satish & @megha : in exp p=(char *)&a;
p is a pointer of type char and a is a interger but a pointer is always of type unsigned constant interger so what we are doing is typecasting the address so that so that the compatability remains........
p is a pointer of type char and a is a interger but a pointer is always of type unsigned constant interger so what we are doing is typecasting the address so that so that the compatability remains........
Well wisher said:
1 decade ago
Preethi's explanation is reall good.
Some asked why not D ?.
If you see the things.
0102
02 is in separate address.
O1 is in very next address.
In case of little endian.
To know what exactly happens refer to some microprocessor book it will give you a clear idea.
Some asked why not D ?.
If you see the things.
0102
02 is in separate address.
O1 is in very next address.
In case of little endian.
To know what exactly happens refer to some microprocessor book it will give you a clear idea.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers