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

  1. Path: sparky!uunet!ukma!usenet.ins.cwru.edu!agate!doc.ic.ac.uk!anb
  2. From: anb@doc.ic.ac.uk (A N Burton)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to print an integer as binary?
  5. Date: 5 Nov 1992 18:33:44 GMT
  6. Organization: Department of Computing, Imperial College, University of London, UK.
  7. Lines: 86
  8. Distribution: world
  9. Message-ID: <1dbpe8INNcop@frigate.doc.ic.ac.uk>
  10. References: <1992Nov4.180622.6568@csd.uwe.ac.uk>
  11. NNTP-Posting-Host: mfast.doc.ic.ac.uk
  12.  
  13. In article <1992Nov4.180622.6568@csd.uwe.ac.uk> th2_oate@csd.uwe.ac.uk (Tom Oates) writes:
  14. >Assuming that:- 
  15. >
  16. >int i = 203;
  17. >
  18. >How do I display 'i' as 11001011?
  19. >
  20. >
  21. >
  22. >
  23. >-- 
  24. >****************************************************************************** 
  25. >* Tom Oates (Univ. of West of England) * DISCLAIMER:I speak only for myself. * 
  26. >* Email to: th2_oate@uk.ac.uwe.csd     * PGP 2.0 Key available upon request. *
  27. >******************************************************************************
  28.  
  29.  
  30. The program appearing below gives three possible solutions; the first is for
  31. those favouring recursion, the other two use loops.  The second scans the
  32. number from right to left, and therefore uses an array to hold the partial
  33. results.  Solution three scans from left to right and thus outputs as the
  34. binary form is generated.
  35.  
  36.  
  37. Ariel BURTON
  38.  
  39.  
  40.  
  41. #include    <stdio.h>
  42.  
  43.  
  44. void
  45. PrintBinary1( long x ){
  46.     if ( x < 0 ){
  47.         putchar( '-' );
  48.         PrintBinary1( -x );
  49.     }
  50.     else if( x < 2 )
  51.         putchar( '0'+x );
  52.     else{
  53.         PrintBinary1( x >> 1 );
  54.         putchar( '0'+( x & 1 ) );
  55.     }
  56. }
  57. #define MAX_SIZE 32
  58. void
  59. PrintBinary2( long x ){
  60.     char    buff[ MAX_SIZE+1 ];
  61.     int    i;
  62.  
  63.     if( x < 0 ){
  64.         x = -x ;
  65.         putchar( '-' );
  66.     }
  67.  
  68.     buff[ i = MAX_SIZE ] = '\0';
  69.     for( ; x ; x >>= 1 )
  70.         buff[ --i ] = '0'+ (x&1);
  71.  
  72.     printf( "%s", buff+i );
  73. }
  74.  
  75. void
  76. PrintBinary3( long x ){
  77.     unsigned long    mask = 1 << (8*sizeof(unsigned long) - 1);
  78.  
  79.     if( x < 0 ){
  80.         putchar( '-' );
  81.         x = -x;
  82.     }
  83.  
  84.     for( ;  ! (x & mask) ; mask >>= 1 )
  85.         ;
  86.  
  87.     for( ; mask ; mask >>= 1 )
  88.         putchar( x & mask ? '1' : '0' );
  89. }
  90.  
  91. main( int argc, char *argv[] ){
  92.     PrintBinary1( atol(argv[1] ) );
  93.     putchar( '\n' );
  94.     PrintBinary2( atol(argv[1] ) );
  95.     putchar( '\n' );
  96.     PrintBinary3( atol(argv[1] ) );
  97.     putchar( '\n' );
  98. }
  99.