home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / languags / c / checkbts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  2.6 KB  |  98 lines

  1. 17-Feb-86 14:37:31-MST,2686;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-SMOKE.ARPA by SIMTEL20.ARPA with TCP; Mon 17 Feb 86 14:37:25-MST
  4. Received: from brl-aos.arpa by SMOKE.BRL.ARPA id a013663; 15 Feb 86 21:42 EST
  5. Received: from ames-nas-gw.arpa by AOS.BRL.ARPA id a012214; 15 Feb 86 21:32 EST
  6. From: Marty <fouts@ames-nas.arpa>
  7. Message-Id: <8602160233.AA11868@ames-nas.ARPA>
  8. Received: by ames-nas.ARPA; Sat, 15 Feb 86 18:33:58 pst
  9. Date: 15 Feb 1986 1833-PST (Saturday)
  10. To: unix-sources@BRL.ARPA
  11. Cc: fouts@ames-nas.arpa
  12. Subject: How many bits and where they go.
  13.  
  14.  
  15.      Attached is a simple minded program which gives a printout of various
  16. odd bits of information about the way a machine does arithmetic in C.
  17.  
  18.      It works on a number of unix implementations (I. E. gives the same
  19. answers as the local C manual,) and I would be very interested in hearing
  20. from anyone who tries it on other implementations.
  21.  
  22.      (So far, I've tried it on SVR2 and BSD4.2 Vaxen, SGI Irises, Amdahl
  23. UTS, and Cray UniCos.)
  24.  
  25.      Compile in the usual way.
  26.  
  27. ----- Cut here and compile.  THIS IS NOT A SHELL SCRIPT -----
  28.  
  29. #define checkbits(N,CP,L) { \
  30.     N = 1; \
  31.     L = 0; \
  32.     while (N != 0) { \
  33.     N <<= 1; \
  34.     L++; \
  35.     } \
  36.     printf("%s   %4d   %6d    ", CP, L, sizeof(N)); \
  37.     N = 1; \
  38.     N = ~N; \
  39.     switch (N) { \
  40.         case -1 : printf("1's c.\n"); break; \
  41.     case -2 : printf("2's c.\n"); break; \
  42.     default : if (N < 0) { \
  43.         printf("  mag.\n"); \
  44.     } else { \
  45.         printf("unsign\n"); \
  46.     } \
  47.     } \
  48. }
  49.  
  50. #define printbytes(CP,N,L) { \
  51.     long temp = (long) N; \
  52.     printf("%s ",CP); \
  53.     for (i = 0; i < ((L) / bits); i++) { \
  54.         printf("%3d",(int) (temp & 0xFFL)); \
  55.     temp >>= bits; \
  56.     } \
  57.     printf("\n"); \
  58. }
  59.  
  60. char C;
  61. short S;
  62. int I;
  63. long L;
  64. int icl, isl, iil, ill;
  65.  
  66. union u {
  67.     char C;
  68.     short S;
  69.     int I;
  70.     long L;
  71.     char FILLER[sizeof(L)];
  72. } U;
  73.  
  74.  
  75. main()
  76. {
  77.     int i;
  78.     int bits;
  79.     for (i = 0; i < sizeof(U.L); i++) U.FILLER[i] = (char) (i + 1);
  80.     printf(" Type   Bits   Sizeof   Format\n");
  81.     checkbits(C," Char",icl);
  82.     bits = icl;
  83.     checkbits(S,"Short",isl);
  84.     checkbits(I,"  Int",iil);
  85.     checkbits(L," Long",ill);
  86.     printf("\nPosition ");
  87.     for (i = 0; i < sizeof(U.L); i++) printf("%3d",i);
  88.     printf("\n");
  89.     printbytes("    Char",U.C,icl);
  90.     printbytes("   Short",U.S,isl);
  91.     printbytes("     Int",U.I,iil);
  92.     printbytes("    Long",U.L,ill);
  93.     printf("\n");
  94.     if ( (char) U.S == U.S ) printf("Char == Short\n");
  95.     if ( (short) U.I == U.I ) printf("Short == Int\n");
  96.     if ( (long)((int) (U.L)) == U.L ) printf("Int == Long\n");
  97. }
  98.