반응형

Float형은 Int형과 달리 메모리에 저장될때:

sign bit + Exponent + value 형태로 저장되게 되는데 이 값을 다시 float형으로 출력하려면 %f로 출력하면 되지만 공부를 할 때에 이 실수가 어떻게 저장되는지 보고싶을 때가 있습니다. 이 때에 사용하면 되는 코드입니다. 

 

#include <stdio.h>
#include <stdlib.h>

int bit_return(int a, int loc) // Bit returned at location
{
  int buf = a & 1<<loc;
        if (buf == 0)
                return 0;
        else
                return 1;
}
int main()
{
        float a;
        scanf("%f",&a);
        void *b;
        b = &a;
        int i = 0;
        for (i = 31; i>=0; i--)
        {
                int j =0;
                printf("%d",bit_return(*((int*)b),i));
                j = 31 - i;
                switch(j){
                case 0:
                case 8:
                        printf(" ");
                }
        }
        printf("\n");
        return 0;
}
반응형
블로그 이미지

개발자_무형

,