U
user923005
That's inverting (1 turn into 0 and vice versa), not reverting. I understood
the OP wants the laest significant bit being the most significate, the 2nd
least being the second most and so on.
Bye, Jojo
Actually, if the O.P. had wanted the ORDER of the bits reversed, he
should have asked for that.
Richard's reply was intended to be funny, and it made me laugh.
Of course, anyone with 30 seconds worth of time can do a web search
and find plenty of integer reversal routines.
But the thing that makes them interesting is understanding how they
work.
#include <limits.h>
#include <assert.h>
#define bitset( buf, bit ) ( buf[(bit) >> 3] |= ( 1 << ( (bit) & 7 )))
#define bitclr( buf, bit ) ( buf[(bit) >> 3] &= ~( 1 << ( (bit) &
7 )))
#define bittog( buf, bit ) ( buf[(bit) >> 3] ^= ( 1 << ( (bit) & 7 )))
#define bitget( buf, bit ) ((( buf[(bit) >> 3] >> ( (bit) & 7 )) &
1 ))
/*
http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
*/
unsigned reversebits0(unsigned n)
{
/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);
n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa);
n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc);
n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0);
n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00);
n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000);
return n;
}
/*
http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#ReverseByteWith64BitsDiv
*/
unsigned reversebits1(unsigned n)
{
unsigned char b,
tmp;
unsigned char *p = (unsigned char *) &n;
/* The assumption is that there are 32 usable bits in unsigned int */
assert(UINT_MAX == 4294967295);
/* The assumption is that there are 8 usable bits in unsigned char */
assert(UCHAR_MAX == 255);
/* The assumption is that there are 4 unsigned chars in unsigned int
*/
assert(sizeof n == 4);
b = p[3];
b = ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >>
32;
tmp = p[0];
p[0] = b;
tmp = ((tmp * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL p[3] = tmp;
b = p[2];
b = ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >>
32;
tmp = p[1];
p[1] = b;
tmp = ((tmp * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL p[2] = tmp;
return n;
}
unsigned reversebits2(unsigned n)
{
unsigned n_reversed = 0;
unsigned i;
unsigned char *p = (unsigned char *) &n;
unsigned char *q = (unsigned char *) &n_reversed;
/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);
for (i = 0; i < 32; i++) {
if (bitget(p, i)) bitset(q, 31 - i);
}
return n_reversed;
}
#ifdef UNIT_TEST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void display_bits(unsigned n)
{
unsigned char *p = (unsigned char *) &n;
unsigned i;
/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);
for (i = 0; i < 32; i++) {
if (bitget(p, i)) putchar('1');
else
putchar('0');
}
putchar('\n');
}
int main(void)
{
char string[256];
puts("enter a number between 0 and 4294967295");
fflush(stdin);
if (fgets(string, sizeof string, stdin)) {
unsigned n = (unsigned) atof(string);
unsigned n_rev;
display_bits(n);
n_rev = reversebits0(n);
display_bits(n_rev);
n_rev = reversebits1(n);
display_bits(n_rev);
n_rev = reversebits2(n);
display_bits(n_rev);
}
return 0;
}
#endif