본문 바로가기

C언어/강의

2010년 10월 18일 월요일 (실수 : 지수부 & 가수부 계산)

int A = 0x12345678;

7

8

5

6

3

4

1

2

little indian

 

unsigned int A = 0x12345678;

unsigned short *SP = (short*)&A

unsigned char *CP = (char*)&A

 

printf(“%x\n”, *SP); 5678 출력

printf(“%x\n”, *CP); 78 출력

 

 

 

992

1000

CP

996

1000

SP

1000

7

8

5

6

3

4

1

2

A

 

add

 

ress

메모리(4byte) 





Float example.svg
n = (-1)^s \times
           (m2^{-23})\times
           2^{x - 127}

41c8 000016 = 0100 0001 1100 1000 0000 0000 0000 00002

then we break it down into three parts; sign bit, exponent and significand.

Sign bit: 0
Exponent: 1000 00112 = 8316 = 131
Significand: 100 1000 0000 0000 0000 00002 = 48000016

We then add the implicit 24th bit to the significand

Significand: 1100 1000 0000 0000 0000 00002 = C8000016

and decode the exponent value by subtracting 127

Raw exponent: 8316 = 131
Decoded exponent: 131 - 127 = 4

Each of the 24 bits of the significand, bit 23 to bit 0, represents a value, starting at 1 and halves for each bit, as follows

bit 23 = 1
bit 22 = 0.5
bit 21 = 0.25
bit 20 = 0.125
bit 19 = 0.0625
.
.
bit  0 = 0.00000011920928955078125

The significand in this example has three bits set, bit 23, bit 22 and bit 19. We can now decode the significand by adding the values represented by these bits.

Decoded significand: 1 + 0.5 + 0.0625 = 1.5625 = C80000/223

Then we need to multiply with the base, 2, to the power of the exponent to get the final result

1.5625 × 24 = 25

Thus

41c8 0000   = 25