home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16043 < prev    next >
Encoding:
Text File  |  1992-11-06  |  2.3 KB  |  98 lines

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!beckman.com!dn66!a_rubin
  2. Newsgroups: comp.lang.c
  3. Subject: Re: How to print an integer as binary?
  4. Message-ID: <a_rubin.720993308@dn66>
  5. From: a_rubin@dsg4.dse.beckman.com (Arthur Rubin)
  6. Date: 5 Nov 92 19:55:08 GMT
  7. References: <1992Nov4.180622.6568@csd.uwe.ac.uk> <1dbpe8INNcop@frigate.doc.ic.ac.uk>
  8. Organization: Beckman Instruments, Inc.
  9. Nntp-Posting-Host: dn66.dse.beckman.com
  10. Lines: 86
  11.  
  12. In <1dbpe8INNcop@frigate.doc.ic.ac.uk> anb@doc.ic.ac.uk (A N Burton) writes:
  13.  
  14.  
  15. >#include    <stdio.h>
  16.  
  17.  
  18. >void
  19. >PrintBinary1( long x ){
  20. >    if ( x < 0 ){
  21. >        putchar( '-' );
  22. >        PrintBinary1( -x );
  23.  
  24. usually infinite recursion on a 2s complement machine if x = LONG_MIN;
  25. can be easily fixed only by having a PrintBinary1U (unsigned long)
  26.  
  27. >    }
  28. >    else if( x < 2 )
  29. >        putchar( '0'+x );
  30. >    else{
  31. >        PrintBinary1( x >> 1 );
  32. >        putchar( '0'+( x & 1 ) );
  33. >    }
  34. >}
  35. >#define MAX_SIZE 32
  36.  
  37. perhaps:
  38.  
  39. #include <limits.h>
  40. #define MAX_SIZE  (CHAR_BIT * sizeof(long))
  41.  
  42. >void
  43. >PrintBinary2( long x ){
  44. >    char    buff[ MAX_SIZE+1 ];
  45. >    int    i;
  46.  
  47. >    if( x < 0 ){
  48. >        x = -x ;
  49. >        putchar( '-' );
  50. >    }
  51.  
  52. >    buff[ i = MAX_SIZE ] = '\0';
  53. >    for( ; x ; x >>= 1 )
  54.  
  55. loops forever if x is LONG_MIN, as -LONG_MIN is usually LONG_MIN;
  56. can be fixed by copying x to an unsigned long and using it.
  57.  
  58. >        buff[ --i ] = '0'+ (x&1);
  59.  
  60. >    printf( "%s", buff+i );
  61. >}
  62.  
  63. >void
  64. >PrintBinary3( long x ){
  65. >    unsigned long    mask = 1 << (8*sizeof(unsigned long) - 1);
  66.                                  ^
  67.                                  \CHAR_BIT
  68.  
  69. or, the clean ANSI version:
  70.  
  71.     unsigned long mask = (((unsigned long)(-1)) >> 1) + 1;
  72.  
  73. >    if( x < 0 ){
  74. >        putchar( '-' );
  75. >        x = -x;
  76. >    }
  77.  
  78. >    for( ;  ! (x & mask) ; mask >>= 1 )
  79. >        ;
  80.  
  81. >    for( ; mask ; mask >>= 1 )
  82. >        putchar( x & mask ? '1' : '0' );
  83. >}
  84.  
  85. >main( int argc, char *argv[] ){
  86. >    PrintBinary1( atol(argv[1] ) );
  87. >    putchar( '\n' );
  88. >    PrintBinary2( atol(argv[1] ) );
  89. >    putchar( '\n' );
  90. >    PrintBinary3( atol(argv[1] ) );
  91. >    putchar( '\n' );
  92. >}
  93. --
  94. Arthur L. Rubin: a_rubin@dsg4.dse.beckman.com (work) Beckman Instruments/Brea
  95. 216-5888@mcimail.com 70707.453@compuserve.com arthur@pnet01.cts.com (personal)
  96. My opinions are my own, and do not represent those of my employer.
  97. My interaction with our news system is unstable; please mail anything important.
  98.