Skip to main content
GameDev.net gamedev.net
🔒 Locked

Byte swapping question

Started by Mr Furious Sep 11, 2005 at 11:14 PM 0 replies 450+ views
Original Post
Mr Furious
Mr Furious
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.
MaulingMonkey
MaulingMonkey
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.