Jump to content
  • Advertisement
Sign in to follow this  
Mr Furious

Byte swapping question

This topic is 4819 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've recently been looking into cross platform memory management and was thinking about byte swapping for "endian-ness".
typedef unsigned short u16;

u16 endianSwap16(u16 var)
{
	u16 left  = (((var) & 0xff) << 8);
	u16 right = (((var) >> 8) & 0xff);

	return left | right;
}

My question concerns the "0xff". Why even bother having it? Shouldn't shifting by 8 just push the unwanted bits out of the way anyhow? This seems redundant to me.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Mr Furious
My question concerns the "0xff". Why even bother having it? Shouldn't shifting by 8 just push the unwanted bits out of the way anyhow? This seems redundant to me.


It's a readability issue I'd say, making clear that they're explicitly moving one byte. Ditto with "(var)" instead of "var", although that's actually counterproductive, IMO.

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!