S
Stephen Mayes
Please humor a curious hobbyist.
Below is some code I made to amuse myself and reverse bit order. Any
comments or corrections are welcome, but I have some specific concerns.
I added the #define to try to make this portable. But what if I wanted to
*ensure* that my data type had 32 bits?
Would this be done in pre-processing?
If the 32-bit type turns out *not* to be unsigned long, how could I declare
p and r, and wouldn't my printf's and scanf be in trouble, too?
Are my concerns completely unfounded? Is there ever any valid reason to
*ensure* a specific bit count?
I really don't know where to begin, so a hint or a pointer to some relevant
example might be more fun that actually telling.
gcc -ansi -pedantic -lm rev.c
cat rev.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <limits.h>
#define LONG_BIT (CHAR_BIT * sizeof(unsigned long))
void print_binary( unsigned long n )
{
int i = 0;
for ( i = LONG_BIT - 1; i >= 0; i-- )
putchar( n & 1 << i ? '1' : '0' );
printf( "\n" );
}
int main( void )
{
unsigned long p = 0, r = 0;
int i;
for(;
{
p = pow( 2, LONG_BIT ) - 1;
r = 0;
printf( "\nEnter an integer value 0 to %lu or 'q' to
quit\n",
p );
if ( ! scanf( "%lu", &p ) )
{
if ( toupper( getchar() ) == 'Q' )
break;
puts( "Input error." );
while ( getchar() != '\n' );
continue;
}
while ( getchar() != '\n' );
for ( i = 0; i < LONG_BIT; i++ )
{
if ( p & ( 1 << i ) )
r |= ( 1 << ( LONG_BIT - 1 - i ) );
}
printf( "Value: %lu\n", p );
printf( "%-10s", "Binary:" );
print_binary( p );
printf( "%-10s", "Reversed:" );
print_binary( r );
}
return EXIT_SUCCESS;
}
Below is some code I made to amuse myself and reverse bit order. Any
comments or corrections are welcome, but I have some specific concerns.
I added the #define to try to make this portable. But what if I wanted to
*ensure* that my data type had 32 bits?
Would this be done in pre-processing?
If the 32-bit type turns out *not* to be unsigned long, how could I declare
p and r, and wouldn't my printf's and scanf be in trouble, too?
Are my concerns completely unfounded? Is there ever any valid reason to
*ensure* a specific bit count?
I really don't know where to begin, so a hint or a pointer to some relevant
example might be more fun that actually telling.
gcc -ansi -pedantic -lm rev.c
cat rev.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <limits.h>
#define LONG_BIT (CHAR_BIT * sizeof(unsigned long))
void print_binary( unsigned long n )
{
int i = 0;
for ( i = LONG_BIT - 1; i >= 0; i-- )
putchar( n & 1 << i ? '1' : '0' );
printf( "\n" );
}
int main( void )
{
unsigned long p = 0, r = 0;
int i;
for(;
{
p = pow( 2, LONG_BIT ) - 1;
r = 0;
printf( "\nEnter an integer value 0 to %lu or 'q' to
quit\n",
p );
if ( ! scanf( "%lu", &p ) )
{
if ( toupper( getchar() ) == 'Q' )
break;
puts( "Input error." );
while ( getchar() != '\n' );
continue;
}
while ( getchar() != '\n' );
for ( i = 0; i < LONG_BIT; i++ )
{
if ( p & ( 1 << i ) )
r |= ( 1 << ( LONG_BIT - 1 - i ) );
}
printf( "Value: %lu\n", p );
printf( "%-10s", "Binary:" );
print_binary( p );
printf( "%-10s", "Reversed:" );
print_binary( r );
}
return EXIT_SUCCESS;
}