Jump to content
  • Advertisement
Sign in to follow this  
ShmeeBegek

swapping the bits in a byte

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am wondering about an efficient method to store things in a little "bit" order on x86:
[FEDCBA98][76543210] <--big endian bits
[76543210][FEDCBA98] <- little endian bits
[01234567][89ABCDEF] <- my bits

This seems to require swapping the bits in the bytes of a register, and all the solutions that I've come up with for this are very slow. I don't see anything in the way of an instruction for this, although I'm not incredibly experienced with x86 assembly. (Yes I have googled this) Thanks, ~SPH

Share this post


Link to post
Share on other sites
Advertisement
You do realize that you don't have to swap actual bits around if you're concerned about endian-ness between (say) Mac and Windows? It's only the bytes that you need to worry about.

But if for some reason you really want to do it probably the simplest way is a 256-entry lookup table.

Actually your examples don't make any sense to me either. It's looks like all you're doing is flipping the high bit of each nibble.

Share this post


Link to post
Share on other sites
x86 asm:
bswap reg

C

unsigned short htons(unsigned short x)
{
return x >> 8 | x << 8;
}

unsigned htonl(unsigned x)
{
return htons(x >> 16) | htons(x & 0xFFFF) << 16;
}


edit: this doesn't solve the problem, this is for big<->little endian byte order... the bits are in another castle.

[Edited by - DigitalDelusion on August 15, 2005 6:28:30 PM]

Share this post


Link to post
Share on other sites
var=((var&0x0f)<<4)|((var&0xf0)>>4);
var=((var&0x33)<<2)|((var&0xcc)>>2);
var=((var&0x55)<<1)|((var&0xaa)>>1);

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
Hey sweet, it's another excuse to link to my most favorite page ever! They list a bunch of methods.

+! I'd seen this page a while ago but never kept the link. Some of this stuff really comes in handy when you want to write code that no one else is going to understand =b

Share this post


Link to post
Share on other sites
DigitalDelusion: that doesn't do what he wants.

A lookup table is the most obvious solution, although the 64-bit arithmetic operations are cute.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!