In response to Krunk's comment above:
First of all, you probably want to do this:
short total = tempA | tempB;
You're logically trying to OR them, not add them. If you wanted to do this with inputShort being a pointer, that'd be pretty easy, but you might as well use a reference instead, so you don't have to mess with dereferencing a pointer. Also, you have to be careful with automatic sign extension when you're dealing with signed types. If you're doing a right-shift of a value that has the most significant bit set, you're dealing with a shift of a negative number.. Well, let me just show you an example:
short foo = -5;
Let's assume 16 bit shorts for now.
In two's complement binary that's
1111 1111 1111 1011
If we right-shifted that by 1, we'd get:
1111 1111 1111 1101
That extra 1 on the left hand side is there because of sign extension.
So let's walk through byte-swapping our -5 using your code:
tempA = ((1111 1111 1111 1011) & 0xff00) >> 8
so, the and operation first gives us:
1111 1111 0000 0000
Then right-shift gives us:
1111 1111 1111 1111
That's obviously not what you wanted.
One way you can do this is just use a reinterpret cast. I'm assuming there's a reason your input to swapShort is a short instead of an unsigned short? In any case, I'll leave it as a short:
//I went ahead and modified this to take a short pointer, like Krunk asked.
//Also note we're assuming 16-bit shorts. This'll break on platforms where this isn't the case.
void Game::swapShort(short *inputShort)
{
//Once we start treating it as unsigned, don't have to worry about sign extension.
unsigned short* tmp = (reinterpret_cast<unsigned short*>(inputShort);
unsigned short highByte = ((*tmp & 0xff00) >> 8)
*tmp = highByte | ((*tmp & 0x00ff) << 8);
}
Or if what you're REALLY trying to do is just get this in network byte order, there's an easier way, you can use htons and ntohs:
An example would be:
unsigned short foo = 300;
//htons = host to network short
unsigned short foo_net_byte_order = htons(foo);
The good thing about using htons and its equivalents is, if you're running the code on a big-endian system, htons & ntohl are basically no-ops, but it'll actually swap them on a little endian system. That is, you don't have to figure out the endianness of the system you're running on before figuring out if you need to swap or not. htons knows what network byte order means and what your host byte order is, and it will just handle it for you. There are equivalents for longs as well. See http://msdn.microsoft.com/en-us/library/ms738557%28VS.85%29.aspx
The same function exists on POSIX type operating systems as well, but you'll have to include a different set of headers.