• lua封装的位运算


    1.移位运算基础

     1 --与   同为1,则为1  
     2    
     3 --或   有一个为1,则为1    
     4   
     5 --非   true为 false,其余为true  
     6   
     7 --异或 相同为0,不同为1  
     8   
     9   
    10 --ZZMathBit = {}  
    11   
    12 function ZZMathBit.__andBit(left,right)    --13     return (left == 1 and right == 1) and 1 or 0  
    14 end  
    15   
    16 function ZZMathBit.__orBit(left, right)    --17     return (left == 1 or right == 1) and 1 or 0  
    18 end  
    19   
    20 function ZZMathBit.__xorBit(left, right)   --异或  
    21     return (left + right) == 1 and 1 or 0  
    22 end  
    23   
    24 function ZZMathBit.__base(left, right, op) --对每一位进行op运算,然后将值返回  
    25     if left < right then  
    26         left, right = right, left  
    27     end  
    28     local res = 0  
    29     local shift = 1  
    30     while left ~= 0 do  
    31         local ra = left % 2    --取得每一位(最右边)  
    32         local rb = right % 2     
    33         res = shift * op(ra,rb) + res  
    34         shift = shift * 2  
    35         left = math.modf( left / 2)  --右移  
    36         right = math.modf( right / 2)  
    37     end  
    38     return res  
    39 end  
    40   
    41 function ZZMathBit.andOp(left, right)  
    42     return ZZMathBit.__base(left, right, ZZMathBit.__andBit)  
    43 end  
    44   
    45 function ZZMathBit.xorOp(left, right)  
    46     return ZZMathBit.__base(left, right, ZZMathBit.__xorBit)  
    47 end  
    48   
    49 function ZZMathBit.orOp(left, right)  
    50     return ZZMathBit.__base(left, right, ZZMathBit.__orBit)  
    51 end  
    52   
    53 function ZZMathBit.notOp(left)  
    54     return left > 0 and -(left + 1) or -left - 1  
    55 end  
    56   
    57 function ZZMathBit.lShiftOp(left, num)  --left左移num位  
    58     return left * (2 ^ num)  
    59 end  
    60   
    61 function ZZMathBit.rShiftOp(left,num)  --right右移num位  
    62     return math.floor(left / (2 ^ num))  
    63 end  
    64   
    65 function ZZMathBit.test()  
    66     print( ZZMathBit.andOp(65,0x3f))  --65 1000001    63 111111  
    67     print(65 % 64)  
    68     print( ZZMathBit.orOp(5678,6789))  
    69     print( ZZMathBit.xorOp(13579,2468))  
    70     print( ZZMathBit.rShiftOp(16,3))  
    71     print( ZZMathBit.notOp(-4))  
    72   
    73     print(string.byte("abc",1))  
    74 end  
    75 cclog("aaaaaaa:")  
    76 ZZMathBit.test() 

    [LUA-print] aaaaaaa:

    [LUA-print] 1

    [LUA-print] 1

    [LUA-print] 7855

    [LUA-print] 15535

    [LUA-print] 2

    [LUA-print] 3

    [LUA-print] 97

    2.红点

    1 function GlobalService:hasRedpointEmail( )  
    2     return ZZMathBit.andOp( ServerData.redPointStatus, dyt.RedPointStatus.Email )  ~= 0  
    3 end 
     1 -- 红点状态  
     2 dyt.RedPointStatus = {  
     3     Email = ZZMathBit.lShiftOp( 1, 0 ),  --邮件  
     4     DailyTask = ZZMathBit.lShiftOp( 1, 1),  -- 每日任务  
     5     CommonTask = ZZMathBit.lShiftOp( 1, 2 ), --任务  
     6     Draw = ZZMathBit.lShiftOp( 1, 3 ), --抽卡  
     7     Sign = ZZMathBit.lShiftOp( 1, 4 ), --签到  
     8     Activity = ZZMathBit.lShiftOp( 1, 5 ), --7日活动  
     9     Legion = ZZMathBit.lShiftOp( 1, 6 ), --军团  
    10     Escort = ZZMathBit.lShiftOp( 1, 7 ), --护航  
    11     Collect = ZZMathBit.lShiftOp( 1, 9 ), --委派  
    12     Union = ZZMathBit.lShiftOp( 1 , 10 ), --联动  
    13     SingleRecharge = ZZMathBit.lShiftOp( 1 , 11 ), --限时活动单笔充值  
    14     TotalRecharge = ZZMathBit.lShiftOp( 1 , 12 ), --限时活动累计充值  
    15     TotalCost = ZZMathBit.lShiftOp( 1 , 13 ), --限时活动累计消费  
    16     OilCost = ZZMathBit.lShiftOp( 1 , 14 ), --限时活动燃油消耗  
    17     TotalDraw = ZZMathBit.lShiftOp( 1 , 15 ), --限时活动金币抽卡  
    18     Qming = ZZMathBit.lShiftOp( 1 , 16 ), --限时活动清明好礼  
    19     BaoXiang = ZZMathBit.lShiftOp( 1 , 19 ), --限时活动金币抽卡宝箱  
    20     LevelFast = ZZMathBit.lShiftOp( 1 , 20 ), --快速补给活动  
    21     LevelFastRed = ZZMathBit.lShiftOp( 1 , 21 ), --快速补给活动红点  
    22 }  

    因此都是先左移然后表示一个唯一状态。

    红点状态:利用与运算  同为1,则为1,用一个32位整数来表示活动。

    原文地址:http://blog.csdn.net/themagickeyjianan/article/details/52640807

  • 相关阅读:
    【USACO19Feb-S】伟大的植被恢复The Great Revegetation
    【USACO19Jan-S】山景Mountain View
    读取jar内部文件的方式
    spring cloud nacos 平台搭建——网关搭建
    spring cloud nacos 平台搭建——服务注册
    spring cloud nacos 平台搭建——nacso注册中心搭建
    idea .groovy脚本生成实体类脚本模板
    移动开发思路
    分布式事务五-TC全局事务协调器
    分布式事务四-Seata AT模式-案例
  • 原文地址:https://www.cnblogs.com/AaronBlogs/p/7516270.html
Copyright © 2020-2023  润新知