Notifications
Clear all

[Closed] SwapShort

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;
}

9 Replies

I don’t understand C++ but if you would write that in pseudocode then I might be able to help…

 rdg

maybe you can use bitarray values to mimic bitshifting?
But this is only a guess to subscribe to this thread.

Georg

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.[font=Courier New]shift[/font]), but my problem is about that & operator. I don’t know what he does.

 rdg

& should be bit.and
as && is a logical AND.

Georg

[size=2][color=white]Problem solved.

fn[/color][/size] ShortSwap s = (
b1 = bit.and s 255

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

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

)

Thanks Georg.

 rdg

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

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

thank you.

Georg

No, thank you for the big help Georg.