lua位运算

  1. --[[  
  2. Description:  
  3.     FileName:bit.lua  
  4.     This module provides a selection of bitwise operations.  
  5. History:  
  6.     Initial version created by  阵雨 2005-11-10.  
  7. Notes:  
  8.   ....  
  9. ]]  
  10. --[[{2147483648,1073741824,536870912,268435456,134217728,67108864,33554432,16777216,  
  11.         8388608,4194304,2097152,1048576,524288,262144,131072,65536,  
  12.         32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1}  
  13.         ]]  
  14.   
  15.   
  16. bit={data32={}}  
  17. for i=1,32 do  
  18.     bit.data32[i]=2^(32-i)  
  19. end  
  20.   
  21. function bit:d2b(arg)  
  22.     local   tr={}  
  23.     for i=1,32 do  
  24.         if arg >= self.data32[i] then  
  25.         tr[i]=1  
  26.         arg=arg-self.data32[i]  
  27.         else  
  28.         tr[i]=0  
  29.         end  
  30.     end  
  31.     return   tr  
  32. end   --bit:d2b  
  33.   
  34. function    bit:b2d(arg)  
  35.     local   nr=0  
  36.     for i=1,32 do  
  37.         if arg[i] ==1 then  
  38.         nr=nr+2^(32-i)  
  39.         end  
  40.     end  
  41.     return  nr  
  42. end   --bit:b2d  
  43.   
  44. function    bit:_xor(a,b)  
  45.     local   op1=self:d2b(a)  
  46.     local   op2=self:d2b(b)  
  47.     local   r={}  
  48.   
  49.     for i=1,32 do  
  50.         if op1[i]==op2[i] then  
  51.             r[i]=0  
  52.         else  
  53.             r[i]=1  
  54.         end  
  55.     end  
  56.     return  self:b2d(r)  
  57. end --bit:xor  
  58.   
  59. function    bit:_and(a,b)  
  60.     local   op1=self:d2b(a)  
  61.     local   op2=self:d2b(b)  
  62.     local   r={}  
  63.       
  64.     for i=1,32 do  
  65.         if op1[i]==1 and op2[i]==1  then  
  66.             r[i]=1  
  67.         else  
  68.             r[i]=0  
  69.         end  
  70.     end  
  71.     return  self:b2d(r)  
  72.       
  73. end --bit:_and  
  74.   
  75. function    bit:_or(a,b)  
  76.     local   op1=self:d2b(a)  
  77.     local   op2=self:d2b(b)  
  78.     local   r={}  
  79.       
  80.     for i=1,32 do  
  81.         if  op1[i]==1 or   op2[i]==1   then  
  82.             r[i]=1  
  83.         else  
  84.             r[i]=0  
  85.         end  
  86.     end  
  87.     return  self:b2d(r)  
  88. end --bit:_or  
  89.   
  90. function    bit:_not(a)  
  91.     local   op1=self:d2b(a)  
  92.     local   r={}  
  93.   
  94.     for i=1,32 do  
  95.         if  op1[i]==1   then  
  96.             r[i]=0  
  97.         else  
  98.             r[i]=1  
  99.         end  
  100.     end  
  101.     return  self:b2d(r)  
  102. end --bit:_not  
  103.   
  104. function    bit:_rshift(a,n)  
  105.     local   op1=self:d2b(a)  
  106.     local   r=self:d2b(0)  
  107.       
  108.     if n < 32 and n > 0 then  
  109.         for i=1,n do  
  110.             for i=31,1,-1 do  
  111.                 op1[i+1]=op1[i]  
  112.             end  
  113.             op1[1]=0  
  114.         end  
  115.     r=op1  
  116.     end  
  117.     return  self:b2d(r)  
  118. end --bit:_rshift  
  119.   
  120. function    bit:_lshift(a,n)  
  121.     local   op1=self:d2b(a)  
  122.     local   r=self:d2b(0)  
  123.       
  124.     if n < 32 and n > 0 then  
  125.         for i=1,n   do  
  126.             for i=1,31 do  
  127.                 op1[i]=op1[i+1]  
  128.             end  
  129.             op1[32]=0  
  130.         end  
  131.     r=op1  
  132.     end  
  133.     return  self:b2d(r)  
  134. end --bit:_lshift  
  135.   
  136.   
  137. function    bit:print(ta)  
  138.     local   sr=""  
  139.     for i=1,32 do  
  140.         sr=sr..ta[i]  
  141.     end  
  142.     print(sr)  
  143. end  
  144.   
  145. bs=bit:d2b(7)  
  146. bit:print(bs)                            
  147. -->00000000000000000000000000000111  
  148. bit:print(bit:d2b(bit:_not(7)))           
  149. -->11111111111111111111111111111000  
  150. bit:print(bit:d2b(bit:_rshift(7,2)))      
  151. -->00000000000000000000000000000001  
  152. bit:print(bit:d2b(bit:_lshift(7,2)))      
  153. -->00000000000000000000000000011100  
  154. print(bit:b2d(bs))                      -->     7  
  155. print(bit:_xor(7,2))                    -->     5  
  156. print(bit:_and(7,4))                    -->     4  
  157. print(bit:_or(5,2))                     -->     7  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值