C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
ROTATE.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* ROTATE.C illustrates bit rotation functions including:
 *      _rotl           _rotr           _lrotl          _lrotr
 *
 * The _lrotl and _lrotr functions are not illustrated, but they are the
 * same as _rotl and _rotr except that they work on long integers.
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
char *binstr( int num, char *buffer );      /* Prototype */
 
void main()
{
    int  shift, i, ir, il;
    char tmpbuf[20];
 
    printf( "Enter integer: " );
    scanf( "%d", &i );
    printf( "\n\n" );
 
    /* Display table header for rotates. */
    printf( "%6s    %-7s    %16s    %-7s    %16s\n",
            " ", "Left", " ", "Right", " " );
    printf( "%6s    %7s    %16s    %7s    %16s\n\n",
            "Shift", "Decimal", "Binary", "Decimal", "Binary" );
 
    /* Display table of rotated values. */
    for( shift = 0; shift <= 16; shift++ )
    {
        il = _rotl( i, shift ); ;
        printf( "%5d    %7d    %16s    ", shift, il, binstr( il, tmpbuf ) );
        ir = _rotr( i, shift );
        printf( "%7d    %16s\n", ir, binstr( ir, tmpbuf ) );
    }
}
 
/* Converts integer to string of 16 binary characters. */
char *binstr( int num, char *buffer )
{
    char tmp[17];
    int  len;
 
    memset( buffer, '0', 16 );
    len = strlen( _itoa( num, tmp, 2 ) );
    strcpy( buffer + 16 - len, tmp );
    return buffer;
}
                                    -♦-