C Programming - Floating Point Issues - Discussion

Discussion :: 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;
}

[A]. 40 AC 00 00
[B]. 04 CA 00 00
[C]. 00 00 AC 40
[D]. 00 00 CA 04

Answer: Option C

Explanation:

No answer description available for this question.

Siva said: (Jul 13, 2010)  
Can you please give the explanation in detail?

Padhu said: (Jul 22, 2010)  
Its because the machine in which that program executed remains a little endian machine. See this for explanation:

http://www.indiabix.com/c-programming/floating-point-issues/discussion-146

Preethi said: (Aug 21, 2010)  
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.

Mini said: (Sep 7, 2010)  
I think preethi is correct.

Pradeep said: (Sep 21, 2010)  
Yeah she is correct but in the end only.

Take one more example:

Say we have to store 0102 0304H in memory (where H : for hexadecimal code).

Say initial memory address is 100H. Then

Memory Address | Big endian | Little Endian
100 : | 01 | 04
101 : | 02 | 03
102 : | 03 | 02
103 : | 04 | 01


and in BIG Endian MSB(most significant bit) is stored at lowest address location (as in my example 01 is the MSB) while Little Endian format follows the exactly opposite trend.

So now as the PC's (intel processors) use " LITTLE ENDIAN" byte order, The result is written from bottom to top.

Vikesh said: (Sep 23, 2010)  
There is different number system is binary and hexadecimal. Our Turbo C is machine dependent. So we get this type of result. In real Digital logic we get different result. Because we get resut in binary.

Anuj said: (Oct 8, 2010)  
Preethi's answer is absolutly correct.

Ankit Chandra said: (Oct 18, 2010)  
Thank you preeti. I really needed that ansswer badly.

Subrata said: (Oct 19, 2010)  
Thank you preeti.

Aishwarya said: (Oct 22, 2010)  
I agree with preeti answer.

Vishnu Kant said: (Nov 1, 2010)  
Thanks preethi. Thanks pradeep. :-).

Nikhil said: (Nov 1, 2010)  
Thanks preethi.

Nikhil Kadam said: (Nov 1, 2010)  
Why we tha answer is not 00 00 CA 04?

Reka said: (Nov 8, 2010)  
How to normalise a number?

Sathish said: (Nov 10, 2010)  
Hi, i have a doubt
in the statement

p=(char *)&a;
why we are used (char *) here

Vignesh said: (Nov 25, 2010)  
Hi, can anyone tell me in what way the given program helps us to find the answer for the given question.

Megha Tavrech said: (Nov 26, 2010)  
I didn't understand this.

How it get executed.

P= (char *) &a;.

Thientran said: (Nov 29, 2010)  
@Reka: Standard for Floating-Point Arithmetic is Ieee754

Anand Upadhyay said: (Nov 30, 2010)  
Thanks preeti.

Siri said: (Dec 1, 2010)  
How to normalize a no can any one help me.

Sirisha said: (Dec 1, 2010)  
p=(char *)&a;
satish i thik (char*)this is for type casting as
a is float and p is char

Shiva said: (Dec 10, 2010)  
Thanks preethi...!

Nnnn Bbbbb said: (Dec 12, 2010)  
How do we decide whether to use big endian or small endian?

Pramod Jarhad said: (Dec 18, 2010)  
I think preethi is correct.

Lordy said: (Dec 18, 2010)  
Didn't understand even. Please explain further.

Abhi17188@Gmail.Com said: (Dec 19, 2010)  
If little endian prints from bottom to top, then why D is not correct.

Dhirendra said: (Dec 21, 2010)  
@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........

Muthu said: (Dec 28, 2010)  
Preethi answer is good.

Amit said: (Dec 29, 2010)  
Why it is c? why not d?

Anusha said: (Jan 11, 2011)  
@amith: the ans is C bcoz it is considered byte wise

Shwetha said: (Jan 16, 2011)  
If we go according to preethi the answer should be 00 00 CA 04
but the answer given is 00 00 AC 40

Vishal Pandey said: (Jan 16, 2011)  
Foating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

nd coz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000
p[1] = 00000000 00000000
p[2] = 10101100 00000000
p[2] = 00000000 10000000

=> in hexadecimal form (%02x) :
p[0] = 00
p[1] = 00
p[2] = ac
p[3] = 40

Tushar &Amp; Mitesh said: (Jan 30, 2011)  
In Question it is not mention on which processor program is compiled and
in option Big endian answer is also there hence we can't say what will be the correct answer....

John said: (Feb 7, 2011)  
Can someone tell me how to normalise ?

Well Wisher said: (Feb 10, 2011)  
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.

Raji said: (Feb 24, 2011)  
Thanks preethi.

Vinay said: (Feb 25, 2011)  
Vishal pandey is correct but how you can normalised the last address p[3]=00000000 10000000 as 40.

Rose said: (Feb 26, 2011)  
I cant understand the concept clearly anybody explain clearly.

What is big Indian and little Indian?

How to do normalizaton?

Atul said: (Feb 28, 2011)  
@Mitesh: Intel processors follow little endian. Big endian is followed by some other vendors like motorola...

Vinoth said: (Mar 17, 2011)  
I am also same doubt as Shweta. Why we take every two bits rather than 1 bit.

Satti said: (Mar 30, 2011)  
Satish, p declared as a character pointer,so a is not a character type. so we can covert a type to p char type.

char *p;
int i=10;

p=(char*)i;// type casting is done.

Arun said: (Apr 1, 2011)  
VISHAL explanation is fine.

Kiran Chowdary said: (Apr 14, 2011)  
I agree with preeti, but can u tell me where we use this.

Jitendra Jain said: (Apr 30, 2011)  
I think about this qstn that
it is not well known form
i exicute that in my pc but result come this
but nobody explain me how it work??

Irshad Alam said: (Jun 29, 2011)  
@ Preethi: Thnx for providing nice answer...

clear to understand.

Mohd Ilyas Dcet said: (Jun 29, 2011)  
The above explanations are correct I would like to give an idea about another method of arrangement i.e 'Big-endian' here we can arrange 1234 as 3-4-1-2 or 2-1-4-3 in the memory addresses

Prabha said: (Jun 29, 2011)  
Can any one please tel me how to do normalization ?

Nikita said: (Jul 4, 2011)  
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.

Santosh said: (Jul 8, 2011)  
#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.

Muthulakshmi said: (Jul 18, 2011)  
How will I understand the c program easily I feel the programs are very difficult. It is easy or not and please teach me.

Rajasekaran said: (Jul 22, 2011)  
Thank preeti and pradeep.

How do normaliize ? please say anyone for me.

Gaurav Kumar said: (Jul 23, 2011)  
Floating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

and bcoz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000
p[1] = 00000000 00000000
p[2] = 10101100 00000000
p[2] = 00000000 10000000

=> in hexadecimal form (%02x) :
p[0] = 00
p[1] = 00
p[2] = ac
p[3] = 40

Raj said: (Aug 6, 2011)  
In little endian, lower order bytes will be stored in lower addresses. But how this is the answer?

Sairam said: (Aug 16, 2011)  
Thanks preethi, visual, gaurav.

Rithvika said: (Sep 1, 2011)  
Hey can any one tell the procedure of normalisation?

Surbhi said: (Sep 12, 2011)  
What do you mean by big endian and little endian ?

Himanshu said: (Sep 15, 2011)  
Hi.

Rithvika normalization is one way to reduce the redundancy in table.

Yamuna said: (Sep 16, 2011)  
I have one doubt here we used unsigned char why don't we use signed char ?

Babu said: (Sep 25, 2011)  
Hay can anyone please tell me what is memory normalization?

Shashikant said: (Sep 26, 2011)  
Thank you to solve this problem.

Hardik said: (Sep 27, 2011)  
Thank you very much preety.

Binit said: (Oct 2, 2011)  
Binary equivalent of 5 = 101 & 0.375 = 011
so 5.375= 101.011

Normalization means in the left side of decimal should only have one integer example: normalisation of 101.345 = 1.01345* 100

So normalized form is 1.01011*100

So according to IEEE notation 5.375 can be represented as
0100 0000 1010 1100 this can be expressed as 40AC0000
but intel follows little endianess so ans will be

C). 0000AC40

Kali said: (Oct 3, 2011)  
@ Binit :
" So normalized form is 1.01011*100

So according to IEEE notation 5.375 can be represented as
0100 0000 1010 1100 this can be expressed as 40AC0000 "

how did you convert the 1.01011 no into normalized form 0100 0000 1010 1100 ???

Sarava said: (Dec 4, 2011)  
Thank you preethi.

Yash said: (Dec 6, 2011)  
Someone is asked why we taking 2 byte not 1 byte like 00 00 AC 40 not like 00 CA 04 because processors work on hexadecimal no on octal. So we take 2 byte or 16 bit instead of 1 byte or 8 bit.

@ Preethi
Thank you very much.

Nisha said: (Dec 6, 2011)  
Thanks preethi.

Prabhjot said: (Dec 9, 2011)  
Thank you preethi.

Loveneet said: (Dec 9, 2011)  
Thank you very much preety.

Nandhini said: (Dec 10, 2011)  
Thanks preethi.

Shareen said: (Dec 11, 2011)  
Thank you preethi for your clear explanation.

Divya said: (Dec 18, 2011)  
Preethi is correct.

Hajmal Deen said: (Dec 20, 2011)  
Preethi is correct, but I can't understand, so explain clearly. Please do the needful.

Divya said: (Dec 20, 2011)  
@ Hajmal

They gave the binary equivalent of 5.375 in normal form

0100 -> 4
0000 -> 0

1010 -> A
1100 -> C

0000 -> 0
0000 -> 0

0000 -> 0
0000 -> 0

Malathi said: (Dec 28, 2011)  
Thank you preethi and pradeep.

Hajmal said: (Jan 2, 2012)  
Thanks divya.

Ravi Ranjan said: (Jan 7, 2012)  
Preethi is good.

Jisha Sankar said: (Jan 19, 2012)  
Thanks preethi.

Chithra said: (Feb 16, 2012)  
Thanks preethi and pradeep.

Er.Nitesh Mathur said: (Mar 13, 2012)  
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.

Ragini said: (Mar 20, 2012)  
Thanks preeti.

Bhaskar said: (Mar 23, 2012)  
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.

Suvidha said: (May 28, 2012)  
In the statement p= (char*) &a; we are doing type casting since p is a char pointer you cannot assign float variable to it a pointer variable can hold the address of another variable only if the variable has the same data type of that as pointer variable.

Nazia said: (Jul 11, 2012)  
The format string used for each of the prints is %02X, which I've always interpreted as 'print the supplied int as a hexadecimal value with at least two digits'.

Shyam said: (Jul 18, 2012)  
Understand the memory you will understand it easy

Used is little endian type of memory storage i.e., higher byte at lower address

Data:40 AC 00 00
100:40
101:AC
102:00
103:00

Now pointer to memory location points to 103
If it is used as character pointer it grabs a byte as char pointer is capable of handling only value of its size

On the other end if a integer pointer is used it would give output same as it is


For detailed view try this example,

int x=Ox12345678;
char *cp;
short int *sip;
int *ip;
ip=sip=cp=&x;
printf("%x %x %x",*cp,*sip,*ip);

O/P:0x78 0x5678 0x12345678

Preeti said: (Sep 3, 2012)  
Thank you preeti:).

Chaitanya@Rgukt said: (Sep 10, 2012)  
Can any please explain How to do normalization of floating point?

Shilpa M Raj said: (Feb 7, 2013)  
Normalize:

Given Floating point Number = 5.375

In binary:
5 = (101) and
0.375 = (011)
{ 0.375*2 = 0.750 --> 0
0.750*2 = 1.500 --> 1
0.500*2 = 1.000 --> 1 }

i.e., 5.375 = 101.011

For single precision representation, decimal point is shifted by 2 times.
i.e., 1.01011*2^2 = 1.01011*100 where 2 is true exponent.

According to IEEE, the representation has three fields:
----------------------------
| S | E | F |
----------------------------
S is one bit representing the sign of the number (0/1 & hidden bit)
E is an 8-bit biased integer representing the exponent
F is an unsigned integer

For single precision representation (the emphasis in this class)
n = 23
bias = 127

2 is the true exponent. For the standard form, it needs to be in 8-bit, biased-127 representation.

2
+127
-----
129

129 in 8-bit, unsigned representation is 1000 0001

The mantissa stored (F) is the stuff to the right of the radix point in the normalized form. We need 23 bits of it.

010110 00000000000000000

Put it all together (and include the correct sign bit):

S E F
0 1000 0001 010110 00000000000000000

The values are often given in hex, so here it is

0100 0000 1010 1100 0000 0000 0000 0000
0x 4 0 A C 0 0 0 0

Suma said: (Jul 7, 2013)  
@Shilpa m raj.

1.01011*2^2 = 1.01011*100 where 2 is true exponent.

I didn't understand, how *2^2 become *100.

Praful said: (Jul 25, 2013)  
I think this value stored in this manner.

0100 0000 1010 1100 0000 0000 0000 0000 which is 40ac0000.


float taken 4 byte in memory in program p=(char*)&a casting this to char type nw p(it char pointer its point to i byte value) point to lower byte of the a(5.375) which is 0000 0000.

p[0]-> contain the fist lower byte data 00(because print bye the hexa format specifier withe 2 width)
p[1]-> contain 00
p[2]-> contain ac
p[3]-> contain 40

Then output become ...0000ac40.

Piya said: (Aug 29, 2013)  
Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

For example, a 4 byte, 32-bit integer.


Byte3 Byte2 Byte1 Byte0.


will be arranged in memory as follows:


Base_Address+0 Byte0.
Base_Address+1 Byte1.
Base_Address+2 Byte2.
Base_Address+3 Byte3.


Example :Intel processors use "Little Endian" byte order.


"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.


Base_Address+0 Byte3.
Base_Address+1 Byte2.
Base_Address+2 Byte1.
Base_Address+3 Byte0.


Motorola, Solaris processors use "Big Endian" byte order.

Sudhir said: (Sep 8, 2013)  
Here what is mean by printf("%02x\n", (unsigned char)p[i]); statement, please explain me.

Sayan Bose said: (Sep 17, 2013)  
How is hexadecimal operation taking place in a compiler?

Suresh Gupta said: (Sep 22, 2013)  
We are use database in C or not ?

Sameer Sarkaar said: (Sep 23, 2013)  
What is little endian?

Dheeraj said: (Nov 26, 2013)  
Why we are using %02x? any one can explain me.

Venkatesh said: (Nov 26, 2013)  
#include<stdio.h>
int main()
{
int i=1; // i=0001 it will store in memory like this 00000000 00000000 00000000 00000001
char *p; // p is declared as a char pointer
p=(char*)&i; //here integer is typecasting to char i.e. 00000001
00000000
00000000
00000000 and pointer is always points to starting address.
if(*p)
printf("=====little-endian);
else
printf("=====big-endian);
return 0;
}

Anand said: (Dec 31, 2013)  
Hexadecimal conversion of the variable in the code. That's it nothing more than that.

Tayyaba said: (Jan 10, 2014)  
@Dheeraj. %02X means 1st two bytes.

Ravi Jivani said: (May 11, 2014)  
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.

Sachin said: (Jun 25, 2014)  
Intel processor store in reverse order.

Deepak Naik said: (Jul 28, 2014)  
What is normalised form in C?

Swathi said: (Jul 30, 2014)  
Can you explain the program for me once again and also the execution part also.

Prabhudeva said: (Aug 28, 2014)  
Foating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000.

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000.

And coz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000.
p[1] = 00000000 00000000.
p[2] = 10101100 00000000.
p[2] = 00000000 10000000.

=> in hexadecimal form (%02x) :
p[0] = 00.
p[1] = 00.
p[2] = ac.
p[3] = 40.

Manisha Sehgal said: (Jan 12, 2015)  
Intel chips are usually Little Endian when it comes to store data in data space. But for storing machine code in code segment the same chip is Big Endian.

Pramod Kumar said: (Aug 22, 2015)  
p = (char*)&a;

Can anyone explain meaning of this line?

Shubham Gorde said: (Aug 28, 2015)  
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.

Usman Ali Ar said: (Oct 23, 2015)  
Why it is C than B?

Shyam said: (Feb 4, 2016)  
Hi answer is C please any one can explain?

Rahul said: (Feb 6, 2016)  
0100 -> 4
0000 -> 0

1010 -> A
1100 -> C

0000 -> 0
0000 -> 0

0000 -> 0
0000 -> 0

How to convert this why we are using 4, A, C, 0, 0?

Sargam said: (Mar 28, 2016)  
The data type of 'a' is in 'float' but the pointer variable used for that is in 'char'. So is it possible to take float type value in character data type?

Raji said: (Jul 17, 2016)  
Thank you so much @Preethi.

Megha Verma said: (Aug 12, 2016)  
Thank you so much @Shilpa M.Raj. Your explanation is really good and clear.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.