K
KG
Could any one tell me how to reverse the bits in an interger?
Could any one tell me how to reverse the bits in an interger?
That's inverting (1 turn into 0 and vice versa), not reverting. I understoodRichard Heathfield said:KG said:
int reverse_bits(int n)
{
return ~n;
}
KG said:Could any one tell me how to reverse the bits in an interger?
Could any one tell me how to reverse the bits in an interger?
Michal said:KG said:Could any one tell me how to reverse the bits in an interger?
Use a loop. For instance here you have a pseudocode (foo represents
ith bit):
#v+
output := 0;
FOR i := 0 TO number of bits:
IF input is set
KG said:Could any one tell me how to reverse the bits in an interger?
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.
#include <limits.h> and use CHAR_BIT instead of 8.Torsten Karwoth said:---------snip------------
#include <string.h>
#include <stdio.h>
unsigned int revbits(unsigned int n)
{
unsigned int i, result = 0;
for (i = sizeof(int) * 8; i; --i)
{
result <<= 1;
if (n & 1)
result |= 1;
n >>= 1;
}
return result;
}
int main(void)
{
int i = (sizeof(int) * 8);
while (--i >= 0)
printf("IN:0x%.08x OUT:0x%.08x\n",
1<<i, revbits(1<<i));
return 0;
};
-----------snap----------
tk@localhost ~/test $ gcc -O2 -Wall -o test test.c
tk@localhost ~/test $ ./test
IN:0x80000000 OUT:0x00000001
IN:0x40000000 OUT:0x00000002
IN:0x20000000 OUT:0x00000004
IN:0x10000000 OUT:0x00000008
..............
IN:0x00000004 OUT:0x20000000
IN:0x00000002 OUT:0x40000000
IN:0x00000001 OUT:0x80000000
(the printf doesnt take sizeof(int) into account)
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.
Apparently (looking at the other answers in this thread) only you andI did not understand that: the OP asked for to "reverse the bits in an
interger". This function reverses all bits in the integer. What you
are proposing is reversing the order of the bits, which is different.
Apparently (looking at the other answers in this thread) only you and
Richard understood it that way.
My point is that inverting (see
http://en.wikipedia.org/wiki/Inverse_(logic)) is not the same as
reverting.
I did not understand that: the OP asked for to "reverse the bits in an
interger". This function reverses all bits in the integer. What you
are proposing is reversing the order of the bits, which is different.
Richard Heathfield said:Joachim Schmitz said:
Nevertheless, it demonstrates that the specification was inadequate.
Over 28% of the respondents who had expressed an interpretation at the
time of writing have chosen to interpret "reverse the bits in an
integer" as "reverse the bits in an integer" rather than "reverse the
order of the bits in an integer".
And reverting is not the same as reversing. And reversing is not the
same as reversing the order.
True, incomplete and leaves room for interpretation.Richard Heathfield said:Joachim Schmitz said:
Nevertheless, it demonstrates that the specification was inadequate.
"invers the bits in an integer". (or "invert"?) A bit that is set when readOver 28% of the respondents who had expressed an interpretation at the
time of writing have chosen to interpret "reverse the bits in an
integer" as "reverse the bits in an integer"
That could indeed have been the full specification.rather than "reverse the order of the bits in an integer".
Oops, of course I meant reversing as the OP wrote.And reverting is not the same as reversing.
What else? To me it is the same. What would 'reverse a string' be to you?And reversing is not the same as reversing the order.
osmium said:Most people whose natural language is English would say "invert the *sense*
of the bits", for what you are describing.
http://dictionary.reference.com/browse/reverse
Yes, it is not 100% clear, but the most likely meaning is reversing
the order.
(Anyway, since correct answers to both the meanings of
the question have been giving, arguing on which one was meant,
until the OP tells us, is sort-of waste of time.)
What else? To me it is the same. What would 'reverse a string' be to
you? Turn the characters upside down?
Well, in all this nonsense I have seen nothing that actually
reverses the order of bits in an unsigned int, independent of
sizeof int. So try this:
while (i) {
push(i & 1, stack);
i >>= 1;
}
while (notempty(stack)) {
i <<= 1;
j = pop(stack);
i != j;
}
with suitable declarations, stack, etc.
Anonymous said:First of all you need to know the size of integer, then you should
go somthing like this:
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.