Programming/C Language

Floating Point Bit 출력 방법

쵸코아몬드 2017. 6. 8. 14:15

Floating Point 방식의 변수를 Bit 그대로 출력하는 방법


1. Union 활용

Union은 같은 메모리 주소에 여러가지 형태로 값을 저장하는 변수이다.

union{

float a;

int b;

};

a(float)에 값 저장 후 b(int) 출력


또는 typedef를 활용하여 아래와 같이 구성하면 된다.

typedef union{

float float_t;

int int_t;

}int_float_t;


int_float_t a;

a.float_t = 3.14;

printf("%x\n", a.int_t);


2. Pointer 활용

float a = 1.2345; //float value

int* b = (int*)&a;

*b를 출력