+ Reply to Thread
Results 1 to 3 of 3

Thread: bitwise ops

  1. #1
    Poster of Note Online status: moebius92 is offline Reputation: moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte moebius92 the Neophyte
    Join Date
    Jan 2008
    Posts
    842

    bitwise ops

    Apparently native Lua has no bitwise operators. Also, apparently EffectCategories are bit fields. Any chance bitwise operator support could be added?

  2. #2
    Counter of Stairs Online status: D.H1cks is offline Reputation: D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend D.H1cks the Bounders-friend
    Join Date
    Feb 2007
    Location
    Toronto, Ontario, Canada
    Posts
    302

    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:


    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
    Just change the DhorPlugins part to whatever you need.....

    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.

  3. #3
    Poster of Note Online status: Garan is online now Reputation: Garan has disabled reputation
    Join Date
    Mar 2007
    Posts
    830
    I recently had need of bitwise AND and bitwise OR functions so I wrote these two:
    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
    end
    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.
    Gnashtooth - 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 to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts