home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap12 / bitout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  880 b   |  41 lines

  1. /* bitout.c  -- compiles one way on an IBM-PC and      */
  2. /*              another on a 68000 chip-based machine  */
  3.  
  4. #define CHIP_80286    /* don't define on a 68000 machine */
  5. #include <stdio.h>
  6.  
  7. main()
  8. {
  9.     int num;
  10.  
  11.     printf("Enter an integer number and I will print"
  12.            " it out in binary\nNumber: ");
  13.     
  14.     if (scanf("%d", &num) != 1)
  15.         {
  16.         fprintf(stderr, "Not an integer\n");
  17.         exit(1);
  18.         }
  19.     Bitout(num);
  20. }
  21.  
  22. Bitout(unsigned int num)
  23. {
  24.     int i, j;
  25.     unsigned char *cp;
  26.  
  27.     cp = (char *)#
  28.  
  29. #if defined(CHIP_80286)    /* IBM-PC */
  30.     for (i = 1; i >= 0; --i)
  31. #endif
  32. #if !defined(CHIP_80286)   /* otherwise 68000 machine */
  33.     for (i = 0; i < 4; ++i)
  34. #endif
  35.         {
  36.         for (j = 7; j >= 0; --j)
  37.             putchar((cp[i] & (1 << j)) ? '1' : '0');
  38.         }
  39.     putchar('\n');
  40. }
  41.