473,791 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15

Anyone know of an efficient way of reversing the bits of a word??

Aug 18 '07 #1
7 5104
On Aug 17, 5:36 pm, benn...@hotmail .com wrote:
Anyone know of an efficient way of reversing the bits of a word??
htons() <-ntohs()

Aug 18 '07 #2
be*****@hotmail .com wrote:
Anyone know of an efficient way of reversing the bits of a word??
Not portably, some instructions sets may have a quick way to do this,
otherwise shift and add.

--
Ian Collins.
Aug 18 '07 #3

<be*****@hotmai l.comwrote in message...
Anyone know of an efficient way of reversing the bits of a word??
{ using std::cout; // for NG post
size_t num( 12345 );
// std::bitset<32B its( num ); // #include <bitset>
std::bitset<std ::numeric_limit s<size_t>::digi tsBits( num );

cout<<"size_t num="<<num<<std ::endl;
cout<<"bitset<3 2Bits="<<Bits<< std::endl;
Bits.flip();
cout<<"bitset<3 2Bits="<<Bits<< std::endl;
num = Bits.to_ulong() ; // be careful with sizes.
cout<<"size_t num="<<num<<std ::endl;

size_t num2( 12345 );
cout<<"size_t num2="<<num2<<s td::endl;
num2 = ~num2;
cout<<"size_t num2="<<num2<<s td::endl;
}
/* -output-
size_t num=12345
bitset<32Bits=0 000000000000000 001100000011100 1
bitset<32Bits=1 111111111111111 110011111100011 0
size_t num=4294954950
size_t num2=12345
size_t num2=4294954950
*/

--
Bob R
POVrookie
Aug 18 '07 #4
be*****@hotmail .com wrote:
Anyone know of an efficient way of reversing the bits of a word??
Reverse all the bits in a 32 bit word:

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) ;
Aug 18 '07 #5
Wow! That is slick, and it worked nicely too!

Not quite sure how it works, but for 16 bits, would it work something
like:

n = ((n > 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
n = ((n > 2) & 0x3333) | ((n << 2) & 0xcccc) ;
n = ((n > 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;
Juha Nieminen wrote:
be*****@hotmail .com wrote:
Anyone know of an efficient way of reversing the bits of a word??

Reverse all the bits in a 32 bit word:

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) ;
Aug 20 '07 #6
On Aug 21, 12:55 am, benn...@hotmail .com wrote:
Wow! That is slick, and it worked nicely too!

Not quite sure how it works, but for 16 bits, would it work something
like:

n = ((n > 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
n = ((n > 2) & 0x3333) | ((n << 2) & 0xcccc) ;
n = ((n > 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;
Textbook example; and please dont top-post...
template<typena me T>
T reverseBits(T inval){

T retval=0;
numberofbits= sizeof(T) * CHAR_BITS;
for ( int i = 0; i < numberofbits; ++i ) {
retval = ( retval << 1 ) | ( inval & 1 );
inval >>= 1;
}
return retval;
}

Aug 20 '07 #7
be*****@hotmail .com wrote:
Wow! That is slick, and it worked nicely too!

Not quite sure how it works, but for 16 bits, would it work something
like:

n = ((n > 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
n = ((n > 2) & 0x3333) | ((n << 2) & 0xcccc) ;
n = ((n > 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;
I believe it additionally needs:

n = ((n > 8) & 0x00ff) | ((n << 8) & 0xff00) ;
Aug 22 '07 #8

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

Similar topics

4
5975
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
3
4021
by: deanfamily11 | last post by:
I need to write a function that takes an integer as a parameter and then returns it reversed (i.e. input: 45678, returned: 87654). Can anyone help?
4
2139
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) { /* main bulk of code goes here */ } }
3
551
by: flipflop | last post by:
Sorry if this is a very faq - I haven't seen the answer here though. The quicksort code below sorts an array from large to small. I simply want it to reverse the sort order: small to large, but can't see how. I've walked through it on paper but am just not seeing it. qsort(double *item,int array_size) { qsort_recurse(item,0,array_size-1); }
1
1464
by: CSN | last post by:
Is it possible to reverse the order of items returned from a plpgaql function that uses RETURN NEXT? (order by col isn't what I want) select * from my_function(); __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free web site building tool. Try it!
11
8095
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in a backcolor/forecolor/etc property and calculate the "reverse colour"? In other words, If a user picks 255 (red) for a control backcolor, I'd like to be able to calculate the opposite or negative of that colour and assign the control's...
6
1874
by: neo | last post by:
Hello everyone, I want to write a function that takes a simple text file as input and outputs a file such that the last line in the input file becomes the first line in the output file and the first line of the input becomes the last line of output. I had written this function as: void fileReverse(char* inputFileName, char *outputFileName) {
5
4160
by: Candace | last post by:
I am writing an encryption program that takes a digit and yields its modulus. For example, 7 Mod 4 yields 3. How can I reverse this procedure and get the original digit back in order to write a decryption program?
8
4764
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
0
9669
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
9515
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
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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
7537
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
6776
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();...
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
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.