home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-25  |  1.4 KB  |  50 lines

  1. /* ROTATE.C illustrates bit rotation functions including:
  2.  *      _rotl           _rotr           _lrotl          _lrotr
  3.  *
  4.  * _lrotl and _lrotr are not shown in the program, but they are the
  5.  * same as _rotl and _rotr except that they work on long integers.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. char *binstr( int num, char *buffer );      /* Prototype */
  13.  
  14. main()
  15. {
  16.     int  shift, i, ir, il;
  17.     char tmpbuf[20];
  18.  
  19.     printf( "Enter integer: " );
  20.     scanf( "%d", &i );
  21.     printf( "\n\n" );
  22.  
  23.     /* Display table header for rotates. */
  24.     printf( "%6s    %-7s    %16s    %-7s    %16s\n",
  25.             " ", "Left", " ", "Right", " " );
  26.     printf( "%6s    %7s    %16s    %7s    %16s\n\n",
  27.             "Shift", "Decimal", "Binary", "Decimal", "Binary" );
  28.  
  29.     /* Display table of rotated values. */
  30.     for( shift = 0; shift <= 16; shift++ )
  31.     {
  32.         il = _rotl( i, shift ); ;
  33.         printf( "%5d    %7d    %16s    ", shift, il, binstr( il,  tmpbuf ) );
  34.         ir = _rotr( i, shift );
  35.         printf( "%7d    %16s\n", ir, binstr( ir, tmpbuf ) );
  36.     }
  37. }
  38.  
  39. /* Convert integer to string of 16 binary characters. */
  40. char *binstr( int num, char *buffer )
  41. {
  42.     char tmp[17];
  43.     int  len;
  44.  
  45.     memset( buffer, '0', 16 );
  46.     len = strlen( itoa( num, tmp, 2 ) );
  47.     strcpy( buffer + 16 - len, tmp );
  48.     return buffer;
  49. }
  50.