473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reverse the bits in an interger?

KG
Could any one tell me how to reverse the bits in an interger?

Jun 20 '07 #1
40 36421
KG said:
Could any one tell me how to reverse the bits in an interger?
int reverse_bits(int n)
{
return ~n;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #2
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Jb******************************@bt.com...
KG said:
>Could any one tell me how to reverse the bits in an interger?

int reverse_bits(int n)
{
return ~n;
}
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
Jun 20 '07 #3
KG <Kr**************@gmail.comwrites:
Could any one tell me how to reverse the bits in an interger?
Use a loop. For instance here you have a pseudocode (foo[i] represents
ith bit):

#v+
output := 0;
FOR i := 0 TO number of bits:
IF input[i] is set
set output[number of bits - i];
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Jun 20 '07 #4
On Jun 20, 2:40 pm, KG <Kriti.Goyal.t...@gmail.comwrote:
Could any one tell me how to reverse the bits in an interger?
First of all you need to know the size of integer, then you should go
somthing like this:

<snip>
int cnt=0,num=0,temp=0,res=0;
while(cnt<32) // here i consider size of integer to be 32-bit
{
temp=num & 1; // num contains the value of integer
res=res | temp;
num=0;
num=num>>1;
res=res<<1;
cnt++;
}
</snip>

I havent checked it. But still the idea will like this only. Also
check for the 32 iteration.
Thanks and Regards,
ar
--
"Let's plan a murder, or start a new religion."
Jun 20 '07 #5
Michal Nazarewicz wrote:
KG <Kr**************@gmail.comwrites:
>Could any one tell me how to reverse the bits in an interger?

Use a loop. For instance here you have a pseudocode (foo[i] represents
ith bit):

#v+
output := 0;
FOR i := 0 TO number of bits:
Suppose we have 2-bit integers, so number of bits is 2; and assuming
the usual reading of a FOR-TO loop ...
IF input[i] is set
.... there will be an array indexing problem, since `i` will take on
the values 0, 1, 2 and either the index `0` or `2` will be wrong.
I suspect you want `number of bits - 1`. Or even better

for i from lowestBitIndex to highestBitIndex do

or, since the ordering doesn't matter

for i in validIndexes do

where `validIndexes` is the set of legal index values.

Encoding these is C is of course straightforward.

--
Chris "unless I play a Rune card" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 20 '07 #6
KG wrote:
Could any one tell me how to reverse the bits in an interger?
---------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)
Jun 20 '07 #7
Joachim Schmitz said:
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Jb******************************@bt.com...
>KG said:
>>Could any one tell me how to reverse the bits in an interger?

int reverse_bits(int n)
{
return ~n;
}
That's inverting (1 turn into 0 and vice versa), not reverting.
It reverses the sense of each bit.
I understood the OP wants the laest significant bit being the most
significate, the 2nd least being the second most and so on.
And so you have (unwittingly?) made explicit the implicit lesson that
specifications *matter*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #8

"Torsten Karwoth" <ag******@gmx.deha scritto nel messaggio news:5d*************@mid.uni-berlin.de...
KG wrote:
>Could any one tell me how to reverse the bits in an interger?

---------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)
#include <limits.hand use CHAR_BIT instead of 8.
Also, what if there are padding bits?
To be *totally* portable: (yes, most machines have 8-bit bytes and
no padding in unsigned int's, but that's not required by the C
standard)
#include <limits.h>

int count_bits(unsigned int n)
{
int result = 1;
while (n /= 2)
result++;
return result;
}

unsigned int revbits(unsigned int n)
{
unsigned int i, result = 0;
for (i = count_bits(UINT_MAX); 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)
printf("IN:0x%.*x OUT:0x%.*x\n", CHAR_BIT * sizeof(int) / 4,
i<<i, CHAR_BIT * sizeof(int) / 4, revbits(1<<i));
Jun 20 '07 #9
On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
int reverse_bits(int n)
{
return ~n;
}

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.
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.

Kind regards,
Johan Borkhuis

Jun 20 '07 #10
<bo******@gmail.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
int reverse_bits(int n)
{
return ~n;
}

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.

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.
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_%28logic%29) is not the same as
reverting.

Bye, Jojo
Jun 20 '07 #11
Joachim Schmitz said:
<bo******@gmail.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
>On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>int reverse_bits(int n)
{
return ~n;
}

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.

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.
Apparently (looking at the other answers in this thread) only you and
Richard understood it that way.
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".
My point is that inverting (see
http://en.wikipedia.org/wiki/Inverse_%28logic%29) is not the same as
reverting.
And reverting is not the same as reversing. And reversing is not the
same as reversing the order.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #12
<bo******@gmail.comwrote:
On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
int reverse_bits(int n)
{
return ~n;
}

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.

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.
Most people whose natural language is English would say "invert the *sense*
of the bits", for what you are describing.
Jun 20 '07 #13

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio news:Wt******************************@bt.com...
Joachim Schmitz said:
><bo******@gmail.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googleg roups.com...
>>On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
int reverse_bits(int n)
{
return ~n;
}

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.

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.
Apparently (looking at the other answers in this thread) only you and
Richard understood it that way.

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".
>My point is that inverting (see
http://en.wikipedia.org/wiki/Inverse_%28logic%29) is not the same as
reverting.

And reverting is not the same as reversing. And reversing is not the
same as reversing the order.
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.)
Jun 20 '07 #14
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Wt******************************@bt.com...
Joachim Schmitz said:
><bo******@gmail.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googleg roups.com...
>>On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
int reverse_bits(int n)
{
return ~n;
}

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.

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.
Apparently (looking at the other answers in this thread) only you and
Richard understood it that way.

Nevertheless, it demonstrates that the specification was inadequate.
True, incomplete and leaves room for interpretation.
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"
"invers the bits in an integer". (or "invert"?) A bit that is set when read
left to right is still set when read right to left, so revers wouldn't be
the right word. But hell, this isn't my native language...
rather than "reverse the order of the bits in an integer".
That could indeed have been the full specification.
Time for the OP to show up and confirm.
>My point is that inverting (see
http://en.wikipedia.org/wiki/Inverse_%28logic%29) is not the same as
reverting.

And reverting is not the same as reversing.
Oops, of course I meant reversing as the OP wrote.
And reversing is not the same as reversing the order.
What else? To me it is the same. What would 'reverse a string' be to you?
Turn the characters upside down? 8-)

Bye, Jojo
Jun 20 '07 #15
Army1987 said:
>
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:Wt******************************@bt.com...
>Joachim Schmitz said:
<snip>
>>My point is that inverting (see
http://en.wikipedia.org/wiki/Inverse_%28logic%29) is not the same as
reverting.

And reverting is not the same as reversing. And reversing is not the
same as reversing the order.

http://dictionary.reference.com/browse/reverse
Yes, it is not 100% clear, but the most likely meaning is reversing
the order.
To your mind, maybe. But it makes much more sense to write the
specification in a way that makes the requirement clear, right from the
start, to *everybody*.
(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.)
I am not, and have not been, arguing about which one the OP meant. I am
arguing that a clear specification is a sine qua non of a correct
program.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #16
osmium wrote:
<bo******@gmail.comwrote:
>On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.dewrote:
>int reverse_bits(int n)
{
return ~n;
}

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.

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.

Most people whose natural language is English would say "invert the *sense*
of the bits", for what you are describing.
I'm not most people [1], but I think you're wrong.
For those with any interest in the matter at all,
I think they'd say "invert the bits", with
no nonsense about "sense". Or they'd use "flip".
Or "reverse". Or "complement".

I rather suspect that the OP meant "reverse
the order of the bits", but experience in
support on another mailing list suggests that
users asking about something different from
what they /say/ they're asking about
happens often enough that mere suspecting
doesn't bake the biscuit.

[1] For which we're all grateful.

--
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 20 '07 #17
Joachim Schmitz said:
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Wt******************************@bt.com...
<snip>
>And reversing is not the same as reversing the order.
What else? To me it is the same. What would 'reverse a string' be to
you? Turn the characters upside down? 8-)
It could reasonably be interpreted as ROT-((UCHAR_MAX + 1)/2).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #18
Anonymous wrote:
On Jun 20, 2:40 pm, KG <Kriti.Goyal.t...@gmail.comwrote:
>Could any one tell me how to reverse the bits in an interger?

First of all you need to know the size of integer, then you should
go somthing like this:
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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 20 '07 #19
On Wed, 20 Jun 2007 10:55:40 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>Anonymous wrote:
>On Jun 20, 2:40 pm, KG <Kriti.Goyal.t...@gmail.comwrote:
>>Could any one tell me how to reverse the bits in an interger?

First of all you need to know the size of integer, then you should
go somthing like this:

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.

You have just exposed a second and much more serious ambiguity, to wit,
what precisely is meant by integer and how is it delimited. The issue is
what determines the number of leading zeroes and the length of the answer.
To illustrate suppose that our "integer" is 00110001 (8 bit words). Is the
correct answer 10001100 or 100011 or 00100011?
Jun 20 '07 #20
On Jun 20, 2:56 am, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
"Richard Heathfield" <r...@see.sig.invalidschrieb im Newsbeitragnews:Jb******************************@b t.com...KG said:
Could any one tell me how to reverse the bits in an interger?
int reverse_bits(int n)
{
return ~n;
}

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/...eWith64BitsDiv
*/
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
>32;
p[3] = tmp;
b = p[2];
b = ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >>
32;
tmp = p[1];
p[1] = b;
tmp = ((tmp * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL
>32;
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

Jun 20 '07 #21
user923005 said:

<snip>
int main(void)
{
char string[256];
puts("enter a number between 0 and 4294967295");
fflush(stdin);
Tell me you meant stdout. Pretty please?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #22
"user923005" writes:
Richard's reply was intended to be funny, and it made me laugh.
That's pretty darn sad. This is supposed to be a technical group to help
people, not a bunch of misfits trying to purposely misunderstand and twist
every little thing that comes along for their own purposes.

Since you enjoyed it, maybe you could set up an alt.hazing group?
Jun 20 '07 #23
osmium <r1********@comcast.netwrote:
That's pretty darn sad. This is supposed to be a technical group to help
people, not a bunch of misfits trying to purposely misunderstand and twist
every little thing that comes along for their own purposes.
It's also not supposed to be a group where posts consisting solely of
questions smacking of homework assignments get serious responses.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 20 '07 #24
Christopher Benson-Manica wrote:
osmium <r1********@comcast.netwrote:
That's pretty darn sad. This is supposed to be a technical group
to help people, not a bunch of misfits trying to purposely
misunderstand and twist every little thing that comes along for
their own purposes.

It's also not supposed to be a group where posts consisting solely of
questions smacking of homework assignments get serious responses.
I agree, but I also agree with osmium. Just SAY, "we won't do your
homework". These interpretation games aren't particularly amusing to
me, and are just confusing to the newbies. Say what you mean, mean what
you say, yadda yadda.


Brian
Jun 20 '07 #25
On Jun 20, 12:40 pm, Richard Heathfield <r...@see.sig.invalidwrote:
user923005 said:

<snip>
int main(void)
{
char string[256];
puts("enter a number between 0 and 4294967295");
fflush(stdin);

Tell me you meant stdout. Pretty please?
*cough* errmmm... What he said.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Jun 20 '07 #26
On Jun 20, 1:04 pm, "osmium" <r124c4u...@comcast.netwrote:
"user923005" writes:
Richard's reply was intended to be funny, and it made me laugh.

That's pretty darn sad. This is supposed to be a technical group to help
people, not a bunch of misfits trying to purposely misunderstand and twist
every little thing that comes along for their own purposes.
The O.P.'s question did get answered, very completely. I think that
responses tend to fit the post rather well.
Richard's function reversed the bits in an integer. I also posted
code to reverse the order of bits in an integer (three different
ways).
Since you enjoyed it, maybe you could set up an alt.hazing group?
Not a bad idea. I got a pretty good hazing when I started posting
here and it was probably one of the best things that ever happened to
me. However, I think that the hazing posters get right here for
making stupid posts is probably good enough that we do not need to
create an entire separate group for it.

Jun 20 '07 #27
On Jun 20, 2:04 pm, "Default User" <defaultuse...@yahoo.comwrote:
Christopher Benson-Manica wrote:
osmium <r124c4u...@comcast.netwrote:
That's pretty darn sad. This is supposed to be a technical group
to help people, not a bunch of misfits trying to purposely
misunderstand and twist every little thing that comes along for
their own purposes.
It's also not supposed to be a group where posts consisting solely of
questions smacking of homework assignments get serious responses.

I agree, but I also agree with osmium. Just SAY, "we won't do your
homework". These interpretation games aren't particularly amusing to
me, and are just confusing to the newbies. Say what you mean, mean what
you say, yadda yadda.
Some of my favorite posts of all time were Peter Seebach's 'homewurk
anserz'.

Jun 20 '07 #28
osmium said:
"user923005" writes:
>Richard's reply was intended to be funny, and it made me laugh.

That's pretty darn sad. This is supposed to be a technical group to
help people, not a bunch of misfits trying to purposely misunderstand
and twist every little thing that comes along for their own purposes.
It was a joke with a purpose behind it, which I have already made plain
elsethread. If the OP is bright enough to write programs, he should be
bright enough to see not only the joke but also its purpose (especially
since it has now been telegraphed), and thus he will have learned
something that will be invaluable to him over the course of his career.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #29

"user923005" <dc*****@connx.comha scritto nel messaggio news:11**********************@n60g2000hse.googlegr oups.com...
On Jun 20, 2:56 am, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
>"Richard Heathfield" <r...@see.sig.invalidschrieb im Newsbeitragnews:Jb******************************@b t.com...KG said:
>Could any one tell me how to reverse the bits in an interger?
int reverse_bits(int n)
{
return ~n;
}

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.
[snip]
/* The assumption is that there are 32 usable bits in unsigned int */
assert(UINT_MAX == 4294967295);
At runtime?
Wouldn't it make more sense to write
#if UINT_MAX != 4294967295
#error "This program is written for machines with 32-bit ints"
#endif
Jun 21 '07 #30
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Nf******************************@bt.com...
osmium said:
>"user923005" writes:
>>Richard's reply was intended to be funny, and it made me laugh.

That's pretty darn sad. This is supposed to be a technical group to
help people, not a bunch of misfits trying to purposely misunderstand
and twist every little thing that comes along for their own purposes.

It was a joke with a purpose behind it, which I have already made plain
Oh well, apparently I once again managed to have missed your humour ...

Bye, Jojo
Jun 21 '07 #31

"Joachim Schmitz" <no*********@schmitz-digital.deschrieb im Newsbeitrag
news:f5**********@online.de...
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:Nf******************************@bt.com...
>osmium said:
>>"user923005" writes:

Richard's reply was intended to be funny, and it made me laugh.

That's pretty darn sad. This is supposed to be a technical group to
help people, not a bunch of misfits trying to purposely misunderstand
and twist every little thing that comes along for their own purposes.

It was a joke with a purpose behind it, which I have already made plain
Oh well, apparently I once again managed to have missed your humour ...
Guess that'll teach me to never ever again take you serious 8-)

Bye, Jojo
Jun 21 '07 #32
Joachim Schmitz said:
>
"Joachim Schmitz" <no*********@schmitz-digital.deschrieb...
>"Richard Heathfield" <rj*@see.sig.invalidschrieb...
>>>
It was a joke with a purpose behind it, which I have already made
plain
Oh well, apparently I once again managed to have missed your humour
...
Guess that'll teach me to never ever again take you serious 8-)
Humour can be an extraordinarily effective teaching tool. If you don't
benefit from it yourself, take comfort in the fact that others do.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 21 '07 #33

"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:4P*********************@bt.com...
Joachim Schmitz said:
>>
"Joachim Schmitz" <no*********@schmitz-digital.deschrieb...
>>"Richard Heathfield" <rj*@see.sig.invalidschrieb...

It was a joke with a purpose behind it, which I have already made
plain
Oh well, apparently I once again managed to have missed your humour
...
Guess that'll teach me to never ever again take you serious 8-)

Humour can be an extraordinarily effective teaching tool.
Agreed. If it is recognizable as such.

Bye, Jojo
Jun 21 '07 #34
user923005 <dc*****@connx.comwrote:
/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);
One, presumably that constant would be better expressed using bit
shift operators (being careful to navigate the default integer
promotions appropriately). Two, does the code you posted handle the
case where sizeof int is 2 and CHAR_BIT is 16 as well as the obvious
case?

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 21 '07 #35
Joachim Schmitz wrote:
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
>Joachim Schmitz said:
>>"Joachim Schmitz" <no*********@schmitz-digital.deschrieb...
"Richard Heathfield" <rj*@see.sig.invalidschrieb...
>
It was a joke with a purpose behind it, which I have already
made plain
Oh well, apparently I once again managed to have missed your
humour ...

Guess that'll teach me to never ever again take you serious 8-)

Humour can be an extraordinarily effective teaching tool.

Agreed. If it is recognizable as such.
Richard has been known to misfire. An outstanding example was his
fairly recent reply in LISPTH to an (apparent) newbie.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 21 '07 #36
On Jun 21, 8:22 am, Christopher Benson-Manica
<a...@vinland.freeshell.orgwrote:
user923005 <dcor...@connx.comwrote:
/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);

One, presumably that constant would be better expressed using bit
shift operators (being careful to navigate the default integer
promotions appropriately). Two, does the code you posted handle the
case where sizeof int is 2 and CHAR_BIT is 16 as well as the obvious
case?
No, but in debug mode the assert will fire and the programmer will
hopefully be smart enough to fix it.
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

Jun 21 '07 #37
On Jun 21, 12:24 am, "Army1987" <please....@for.itwrote:
"user923005" <dcor...@connx.comha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...
On Jun 20, 2:56 am, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
"Richard Heathfield" <r...@see.sig.invalidschrieb im Newsbeitragnews:Jb******************************@b t.com...KG said:
Could any one tell me how to reverse the bits in an interger?
int reverse_bits(int n)
{
return ~n;
}
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.
[snip]
/* The assumption is that there are 32 usable bits in unsigned int */
assert(UINT_MAX == 4294967295);

At runtime?
There is no check for release mode.
Wouldn't it make more sense to write
#if UINT_MAX != 4294967295
#error "This program is written for machines with 32-bit ints"
#endif
There will be no discernable effect, even in debug mode for a single
integer comparison. I see these two as approximately equivalent.

There is one superiority to the #error method -- if the code is never
tested in debug mode, then the assert will never fire.

Jun 21 '07 #38
user923005 <dc*****@connx.comwrote:
On Jun 21, 8:22 am, Christopher Benson-Manica
<a...@vinland.freeshell.orgwrote:
user923005 <dcor...@connx.comwrote:
assert(UINT_MAX == 4294967295);
does the code you posted handle the
case where sizeof int is 2 and CHAR_BIT is 16 as well as the obvious
case?
No, but in debug mode the assert will fire and the programmer will
hopefully be smart enough to fix it.
Why would the assert fire? Is there any reason why UINT_MAX must
have a different value for sizeof int 2/CHAR_BIT 16 and sizeof int
4/CHAR_BIT 8?

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 21 '07 #39

"Christopher Benson-Manica" <at***@vinland.freeshell.orgha scritto nel messaggio news:f5**********@chessie.cirr.com...
user923005 <dc*****@connx.comwrote:
>/* The assumption is that there are 32 usable bits */
assert(UINT_MAX == 4294967295);

One, presumably that constant would be better expressed using bit
shift operators (being careful to navigate the default integer
promotions appropriately). Two, does the code you posted handle the
case where sizeof int is 2 and CHAR_BIT is 16 as well as the obvious
case?
There was an assert(UCHAR_MAX == 255); right below.
Jun 21 '07 #40
Christopher Benson-Manica <at***@faeroes.freeshell.orgwrote:
Why would the assert fire? Is there any reason why UINT_MAX must
have a different value for sizeof int 2/CHAR_BIT 16 and sizeof int
4/CHAR_BIT 8?
Ah, I see that I missed the assert() that was relevant here. Never
mind...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 21 '07 #41

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
3
by: Laszlo Szijarto | last post by:
In C#, wha't the best way to reveser the bit order of a data type and then convert it back to that datatype? So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray...
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
20
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
4
by: ranjanmg1 | last post by:
I have a unsigned char.. i need to reverse it.. what the easiest way to do it?? i dont want to tap each bit save and restore etc etc.... Is it possible to perform some bitwise operation which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.