VC2005 - smaller type check


Hi Jules,

in order to avoid a smaller type check error (not enabled by default in juce)

you need to change in juce_DataConversions.h the following line.

/** Swaps the byte-order of a 16-bit short. */ inline uint16 swapByteOrder (const uint16 n) throw() { return (uint16) (((n & 0x00FF) << 8) | (n >> 8)); }



Hmm. Don’t fancy adding an operation to that function, as it’s supposed to be as quick as possible, and compilers might fail to optimise it out. How about just

return (uint16) (((uint16) (n << 8)) | (n >> 8));


it won’t work as it assert because you’re loosing information while not stating it explicitly.
They say that this what the compiler will do anyway so…
just enable smaller type check to see the message.



Aghh… Forum rule #1: mis-spelling “lose” and “loose” is strictly forbidden!!

I don’t see what’s wrong with that version - if you explicitly cast it, then why is there a problem? I’ll try it with that setting turned on, I guess…


my bad about “losing”.

The problem I think is that you need to explicitly say that you don’t care
about the upper byte of the int16 which you are going to lose with the shift.


Aghh… Forum rule #2: “bad” is a verb, not a noun!! :wink:

Maybe it just needs a cast for both shifts, like this?

return (uint16) (((uint16) (n << 8)) | (uint16) (n >> 8));


how would you say then “my bad …” ?

the problem is only on the first one because you discard the MSB and the checker just tell you “is this really what you wanted to do ?”


I wouldn’t - it’s one of the lamest-sounding bits of slang that’s appeared in recent years! When I hear it I find myself hanging there, waiting for the rest of the sentence… “your bad… what??”

That’s pretty useless of the checker if it can’t even tell that you’re deliberately truncating it. What about using a static_cast<>?


Well you know, that the kind of thing you hear in the movie and that you keep when you’re not a native English speaker.

n is already an uint16 so the static_cast is useless.
It’s not matter of data size just a matter of MSB squeezing.

That’s the point of the mask (& 0x00FF). You tell the checker: yeah I know what I’m doing :slight_smile:

Activate the check if you want to experiment.


You’re using Visual Studio 2005, right? Why not just use _byteswap_ushort?


I think that s because the code was meant to be portable
but maybe there could be some define.

jules ?


Just look at the 32 bit version and do it similarly:

/** Swaps the byte-order of a 16-bit short. */ inline uint16 swapByteOrder (const uint16 n) throw() { #if JUCE_USE_INTRINSICS // Win32 intrinsics version.. return _byteswap_ushort (n); #else return (uint16) ((n << 8) | (n >> 8)); #endif }

JUCE_USE_INTRINSICS is only set for VS2005, so this should be portable.


yes, good point! And the 64-bit version can use that too, of course… I don’t know why I implemented the 32-bit swap like that and forgot to do the others!