@srestha

mam,

let consider, size of int = size of float = 32 bits = 4 Bytes

union

{

int i;

float f;

}

memory allocated to this union is 4 * 8 bits = 32 bits

that is referred as int, float, which will update last, this is it's correct datatype.

data.i=10 ==> the memory location (32 bits) referred as int, ( if you print it %f, then it is showing something else )

data.f = 220.5 ==> the memory location ( 32 bits ) referred as float. ( if you print it %d, then it is showing something else )

Why this thing happens?

220.5 ===> Floating point ===> Stored as IEEE single precision standard format., 32 bits ==> 1 sign bit, 8 exponent bits , 23 Mantissa Bits

how it is stored ?

(-1)^{s} . (1.M) * 2^{true Exponent}. ===> 220.5 = ( 1101 1101 . 1 )_{2} . = ( 1 . 101 1101 1 )_{2} * 2^{7}.

===> true exponent = 7 ===> exponent =7+127 = 134 = 128 + 6

How this 32 bits look like ?

$\underbrace{0} \underbrace{10000110} \underbrace{10111011000000000000000}$

if you access by %f, then it is correct,

if you access by %d, it will consider these memory (32 bits) as integer

==> (0*2^{0}) + (0*2^{1}) +...+(1*2^{30}) +(0*2^{31}) = ( 2^{30} + 2^{25} + 2^{24}+2^{22}+2^{20}+2^{19}+2^{18}+2^{15}) = 1130135552

let consider, size of int = 4 Bytes and size of float = 8 Bytes

memory allocated to this union is 8 * 8 bits = 64 bits

that is referred as int, float, which will update last, this is it's correct datatype.

data.i=10 ==> the memory location ( LSB 32 bits or MSB 32 bits but it is update which it use ) referred as int, ( if you print it %f, then it is showing something else )

data.f = 220.5 ==> the memory location ( 64 bits ) referred as float. ( if you print it %d, then it is depend upon which 32 bits access ===> either little endian or big endian )

How that output changes ?

220.5 ===> Floating point ===> Stored as IEEE double precision standard format., 64 bits ==> 1 sign bit, 11 exponent bits , 52 Mantissa Bits

how it is stored ?

(-1)^{s} . (1.M) * 2^{true Exponent}. ===> 220.5 = ( 1101 1101 . 1 )_{2} . = ( 1 . 101 1101 1 )_{2} * 2^{7}.

===> true exponent = 7 ===> exponent =7+1023 = 1030 = 1024 + 6

How this 64 bits look like ?

$\underbrace{0} \underbrace{10000000110} \underbrace{10111011000000000000000000000....0}$

if you access by %f, then it is correct,

if you access by %d, it will consider these memory (MSB 32 bits = big-endian ) as integer

==> (0*2^{0}) + (0*2^{1}) +...+(1*2^{30}) +(0*2^{31}) = ( 2^{30} + 2^{22} + 2^{21}+2^{19}+2^{17}+2^{16}+2^{15}+2^{12} )

if it is little endian ===> access least 32 bits ===> 000000000....00 = 0