View Full Version : Bitwise in Mel
01-17-2008, 12:01 AM
Is there a page that talks about Bitwise in Mel... <<, >>, And, Or, XoR? I can't find anything about it in the Maya docs.
01-17-2008, 02:50 AM
there are no bit operations in mel....use python......
01-17-2008, 02:30 PM
If you really need them in MEL (why?) you could write your own. << >> Are essentially multiplications/divisions by 2 (or 2^i for shift i places) so these should be easy.
For And/Or/Xor you could use your implemented shifts to "extract" each "bit" of the number in a loop, and add up a new number for every bit pair that satisfies the rule you need (XOR, OR, AND), something like that:
1011 AND 0101
// Now shift both numbers right and subtract with previous ones to get the "bit".
(1 + 2 + 0 + 8) AND (1 + 0 + 4 + 0) // 0
(2 + 0 + 8) AND (0 + 4 + 0) // 0 + (1 && 1) * 2^0 = 1
(0 + 8) AND (4 + 0) // 1 + (2 && 0) * 2^1 = 1 + 0
(8) AND (0) // 1 + (0 && 4) * 2^2 = 1 + 0 + 0
// 1 + (8 && 0) *2^3 = 1 + 0 + 0 + 0
Result is then 0001 as you would expect.
Edit: Oh, cgtalk's code form just made a total mess out of my explanation. Heh, I hope it's still understandable.
01-17-2008, 04:09 PM
do you really need bitwise operations? as HenryKorol already mentioned those are just usual divisions and multiplications in the binary system.
what do you want to do with them? maybe it would be easier to write a simple function which converts numbers from the decimal system to binary and back?
01-18-2008, 07:33 PM
Actually, I just ended up wrapping the C++ Bitwise commands via the API and exposed them to Mel via a plug-in. I also found out that Python does have Bitwise commands that you can use if you are using Maya 8.5 or on. We're still using 8.0 so... that didn't help me.
Thank you for help on the different idea's.
I can't get into the "why" too much. But I can say it involves doing some Binary Masks with assets in a Database. So the Bitwise commands (AND, OR, and XOR) are important to deciphering the combination. Other then that, because this is a production task, it's better for me to wrap a Bitwise Cmd in C++ then for me to try to write the low level function in mel, incase I mess up the logic.
Bitwise is also nice to have for some Game Development issues like getting textures to be powers of two. You can solve the problem multiple ways, at the time I ended up using Pow(), and Log n/Log 2. But it's faster in some respects to use a Bit operator.
Anyway... as most of you know, there is more then one way to skin a cat, and sometime you need to pick the path that suits your Sprint time period. :)
01-18-2008, 07:33 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.
vBulletin v3.0.5, Copyright ©2000-2014, Jelsoft Enterprises Ltd.