PDA

View Full Version : SwapShort


JackStephen
09-22-2006, 11:02 AM
Hello everyone...

I need to make this C function to maxscript:

short ShortSwap( short s )
{
unsigned char b1, b2;
b1 = s & 255;
b2 = (s >> 8) & 255;
return (b1 << 8) + b2;
}

antonv
09-22-2006, 03:05 PM
I don't understand C++ but if you would write that in pseudocode then I might be able to help...

rdg
09-22-2006, 04:06 PM
maybe you can use bitarray values to mimic bitshifting?
But this is only a guess to subscribe to this thread.

Georg

JackStephen
09-22-2006, 04:11 PM
Well that's my problem too, but I know a litle bit of c++.

I know that "<<" is a bitwise shift left operator and the ">>" is a bitwise shift right operator and maxscript have a bitwise function (bit.shift), but my problem is about that & operator. I don't know what he does.

rdg
09-22-2006, 05:06 PM
& should be bit.and
as && is a logical AND.

Georg

JackStephen
09-22-2006, 05:35 PM
Problem solved.


fn ShortSwap s = (
b1 = bit.and s 255

b2 = bit.and (bit.shift s 8) 255

return ( (bit.shift 8 b1) + b2 )

)

Thanks Georg.

rdg
09-22-2006, 05:47 PM
please tell me when to use this?
I googled it and it seems to be needed to write endian independent code.
Where could one use this in max?

regards,

Georg

JackStephen
09-22-2006, 06:50 PM
I need to export the geometry of 3dsmax to a binary file format and for that format I need to flip the bytes. That's why I need this. :)

rdg
09-22-2006, 07:52 PM
thank you.

Georg

JackStephen
09-22-2006, 08:20 PM
No, thank you for the big help Georg.

CGTalk Moderation
09-22-2006, 08:20 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.