반응형

/* 2바이트 크기의 10진수, 8진수, 16진수를 2진수로 변환하기 */

#include <stdio.h>


union n2b {
  unsigned short num;
  struct b {
    unsigned short b0 : 1;
    unsigned short b1 : 1;
    unsigned short b2 : 1;
    unsigned short b3 : 1;
    unsigned short b4 : 1;
    unsigned short b5 : 1;
    unsigned short b6 : 1;
    unsigned short b7 : 1;
    unsigned short b8 : 1;
    unsigned short b9 : 1;
    unsigned short ba : 1;
    unsigned short bb : 1;
    unsigned short bc : 1;
    unsigned short bd : 1;
    unsigned short be : 1;
    unsigned short bf : 1;
  } bit;
} nb;


int main()
{
  nb.num = 0xFFFF;   /* 65535 */


  printf("%d = 0%o = 0x%X = b%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i\n", nb.num, nb.num, nb.num,
         nb.bit.bf, nb.bit.be, nb.bit.bd, nb.bit.bc, nb.bit.bb, nb.bit.ba, nb.bit.b9, nb.bit.b8,
         nb.bit.b7, nb.bit.b6, nb.bit.b5, nb.bit.b4, nb.bit.b3, nb.bit.b2, nb.bit.b1, nb.bit.b0);


  return 0;
}

반응형

+ Recent posts