1

Function Definition:

bit.swapBytes <integer> <integer byte1> <integer byte2> 

Returns an integer with byte1 and byte2 of value int swapped. Byte 1 is the lowest order byte.

I was looking over the documentation for bit shifting in Max Script and came across this function, but to help understand it better I was wondering if it can be written in bit operators like in Python, so I can better understand what this function is doing. Also, does byte 1 have to be the lowest order byte?

karamazovbros
  • 950
  • 1
  • 11
  • 40

1 Answers1

0

It does exactly how it is written in documentation:

bit.swapBytes <integer> <integer byte1> <integer byte2>

Returns an integer with bytes byte1 and byte2 of value int swapped. Byte 1 is the lowest order byte.

Let's take a look into example: bit.swapBytes example

Note, 0xaa (byte 3) was swapped with 0xbb (byte 2). Here is how you can check it in depth with Calculator:

bit.swapBytes example

Alex Khoroshylov
  • 2,234
  • 1
  • 17
  • 28
  • 1
    For reference, I don't have Max so I couldn't test the function (converting max to another framework). So in terms of bit operators it could be done in multiple steps by saving each byte and bit shifting each to the proper location. – karamazovbros Jan 25 '19 at 21:52
  • There is a special CPU instruction (assembler) to swap bytes order: BSWAP (ref: https://c9x.me/x86/html/file_module_x86_id_21.html) So it is possible to implement it in very efficient way: 1. swap bytes in integer, 2. merge operand and BSWAP result by mask. – Alex Khoroshylov Jan 25 '19 at 22:48