Thread: bitwise ops
-
May 02 2012 08:49 PM #1
bitwise ops
Apparently native Lua has no bitwise operators. Also, apparently EffectCategories are bit fields. Any chance bitwise operator support could be added?
-
May 20 2012 11:07 AM #2
Re: bitwise ops
I ended up adding my own bitop support. I got the code somewhere off the net, don't remember where and then modified it for my plugins use:
Just change the DhorPlugins part to whatever you need.....Code:-- get the actual value of the 1 based index of the bit DhorPlugins.bit = function(p) return 2 ^ (p - 1); end -- Typical call: if hasbit(x, bit(3)) then ... DhorPlugins.hasbit = function(x, p) if(x % (p + p) >= p) then return true; else return false; end end DhorPlugins.setbit = function(x, p) return hasbit(x, p) and x or x + p; end DhorPlugins.clearbit = function(x, p) return hasbit(x, p) and x - p or x; end DhorPlugins.togglebit = function(x, p) if(hasbit(x,p)) then return clearbit(x,p); else return setbit(x,p); end end
But I do agree, it would be nice if it was built into LUA. I think this would be more of a LUA request, than a Turbine API request.
-
Apr 10 2013 09:39 AM #3
I recently had need of bitwise AND and bitwise OR functions so I wrote these two:
They aren't nearly as efficient as a C version would be so it would still be nice if Turbine could add a bit class which supported the functions Dhor previously published as well as bitwise AND, OR and NOT functions.Code:function band(x,y) local x2=math.floor(x/2) local y2=math.floor(y/2) local z=0; if x2~=0 and y2~=0 then z=band(x2,y2)*2 end if math.fmod(x,2)==1 and math.fmod(y,2)==1 then z=z+1 end return z end function bor(x,y) local x2=math.floor(x/2) local y2=math.floor(y/2) local z=0; if x~=0 or y2~=0 then z=bor(x2,y2)*2 end if math.fmod(x,2)==1 or math.fmod(y,2)==1 then z=z+1 end return z endGnashtooth - Rank 10 Warg - My breath's worse than my bite - but what d'ya want? I eat Hobbitsess fer cryin' out loud
Garan - Captain of little note - got parked at a Fell Scrying Pool so long it dried up and blew away
and many, many others...
"No, no, the hamsters are for the forums. The servers run on chinchillas!"-Patience 7/20/2007






Reply With Quote
