컴퓨터공부/C & C++ & STL

union을 이용한 float의 이진 출력

achivenKakao 2009. 4. 3. 01:42

union을 이용하여 float의 이진값을 출력하였다.

float를 U32로 치환하더라도 이진 출력하기란 쉽지 않다.(사실 난 모르겠다)

하지만 union을 이용하면 아주 쉽게 사용할 수 있다. 굉장해!!

+


typedef unsigned long U32;

typedef union __Union_typecast
{
 U32 ullong;
 float ffloat;
}Union_typecast;


int main()
{
 Union_typecast stTypecaster;
 U32 ulcnt, view;

 stTypecaster.ffloat = (float)11.1;

 printf("stTypecaster.ffloat[%f] \n\n", stTypecaster.ffloat);

 for(ulcnt = 31; ulcnt > 0; ulcnt--)
 {
  view = stTypecaster.ullong >> ulcnt;

  // view = stTypecaster.ffloat >> ulcnt;
  //  => illegal, left operand has type 'float'

  printf("%d", view & 1);
 }
 
 return 0;
}