473,809 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse the bits in an interger?

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

Jun 20 '07
40 36498
On Jun 20, 2:56 am, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
"Richard Heathfield" <r...@see.sig.i nvalidschrieb im Newsbeitragnews :Jb************ *************** ***@bt.com...KG said:
Could any one tell me how to reverse the bits in an interger?
int reverse_bits(in t 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(un signed 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(un signed 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_MA X == 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(un signed 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(un signed 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********@com cast.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)gma il.com | don't, I need to know. Flames welcome.
Jun 20 '07 #24
Christopher Benson-Manica wrote:
osmium <r1********@com cast.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.i nvalidwrote:
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...@com cast.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...@com cast.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.goo glegroups.com.. .
On Jun 20, 2:56 am, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
>"Richard Heathfield" <r...@see.sig.i nvalidschrieb im Newsbeitragnews :Jb************ *************** ***@bt.com...KG said:
>Could any one tell me how to reverse the bits in an interger?
int reverse_bits(in t 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

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

Similar topics

47
16583
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 about it. I know basic bit manipulation , shifting left, right and have done
3
15650
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 and then call Array.Reverse(), but how to convert back to byte? How about flipping an odd number of bits, say a group of 4 bits, then converting the 4 bits back to an int equivalent? Thanks you in advance for your help,
10
5400
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 string as an input and print it in reverse order.(don't use bulit-in/library function). Sample Output Enter any string: i m Kashif
20
19802
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
6842
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 will give the reversed char. e.g., unsigned char x = 0x8A am expecting an output after reversal x = 0x51
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.