zoukankan      html  css  js  c++  java
  • [Lua]位运算

    参考链接:https://blog.csdn.net/xiaodaidabin/article/details/7968523

    bit.lua

     1 --lua 位运算
     2 --[[bit.data32:
     3 2147483648,1073741824,536870912,268435456,134217728,67108864,33554432,16777216
     4 ,8388608,4194304,2097152,1048576,524288,262144,131072,65536
     5 ,32768,16384,8192,4096,2048,1024,512,256
     6 ,128,64,32,16,8,4,2,1--]]
     7 
     8 bit = {};
     9 bit.data32 = {};
    10 for i=1,32 do
    11     bit.data32[i] = 2 ^ (32 - i);
    12     print(bit.data32[i]);
    13 end
    14 
    15 --十进制转二进制
    16 function bit:d2b(arg)
    17     local temp = {};
    18     for i=1,32 do
    19         if (arg >= self.data32[i]) then
    20             temp[i] = 1;
    21             arg = arg - self.data32[i];
    22         else
    23             temp[i] = 0;
    24         end
    25     end
    26     return temp;
    27 end
    28 
    29 --二进制转十进制
    30 function bit:b2d(arg)
    31     local temp = 0;
    32     for i=1,32 do
    33         if (arg[i] == 1) then
    34             temp = temp + 2 ^ (32 - i);
    35         end
    36     end
    37     return temp;
    38 end
    39 
    40 --打印二进制
    41 function bit:print(arg)
    42     local temp = "";
    43     for i=1,32 do
    44         temp = temp .. arg[i];
    45     end
    46     print(temp);
    47 end
    48 
    49 --测试
    50 local temp = bit:d2b(8);
    51 bit:print(temp);
    52 print(bit:b2d(temp));

    应用场合:

    一、配置字段缩减

    如果存在多个bool型字段,则可以使用一个int型来代替,一个位代表一个字段。下面提供一个工具,其作用是对指定目录下的csv文件,计算其替代值,并保存在最后一列。

     1 using System.Collections.Generic;
     2 using System.IO;
     3 using System.Text;
     4 using UnityEditor;
     5 using UnityEngine;
     6 
     7 public class BitConvertTool {
     8 
     9     private static string configDir = Application.dataPath + "/Config";
    10 
    11     [MenuItem("Tools/BitConvert")]
    12     static void Convert()
    13     {
    14         string[] filePaths = Directory.GetFiles(configDir, "*.csv", SearchOption.AllDirectories);
    15         for (int i = 0; i < filePaths.Length; i++)
    16         {
    17             string filePath = filePaths[i];
    18             List<List<string>> result = CSVTool.Read(filePath, Encoding.Default);
    19             //CSVTool.Debug(result);
    20 
    21             //获取结果
    22             for (int j = 0; j < result.Count; j++)
    23             {
    24                 double sum = 0;
    25                 List<string> line = result[j];
    26                 for (int k = 0; k < line.Count; k++)
    27                 {
    28                     if ((j != 0) && (k != 0) && (k != (line.Count - 1)))
    29                     {
    30                         string field = line[k];
    31                         int num = int.Parse(field);
    32                         if (num == 1)
    33                         {
    34                             sum = sum + Mathf.Pow(2f, 32 - k);
    35                             //Debug.LogError(Mathf.Pow(2f, 32 - k));
    36                         }
    37                         //Debug.Log(num);
    38                     }
    39                     if ((j != 0) && (k == (line.Count - 1)))
    40                     {
    41                         result[j][k] = sum.ToString();
    42                     }
    43                 }
    44             }
    45 
    46             //写入结果
    47             CSVTool.Write(filePath, Encoding.Default, result);
    48         }
    49         Debug.Log("BitConvert Finish");
    50     }
    51 }

    文件格式如下:

  • 相关阅读:
    C# NPOI 导入与导出Excel文档 兼容xlsx, xls(xf13中已经引用了xlsx的npoi)
    ASP.Net超时时间已到解决办法-
    解决SqlDataSource连接超时的问题
    SqlDataSource控件超时的困惑
    Redis+Keepalived
    Linux Ubuntu 16.04 安装步骤+远程环境
    JDK 安装
    Maven 安装
    CAT 默认密码修改
    CAT 监控搭建
  • 原文地址:https://www.cnblogs.com/lyh916/p/9030358.html
Copyright © 2011-2022 走看看