Lua支持Int64和UInt64

基础知识

c# 位运算符


  • << 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。
  • | 如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。

示例
a = 60;(二进制 0011 1100)
b = 13;(二进制 0000 1101)
a << 2 将得到240,即为1111 0000
a|b 将得到 61,即为0011 1101


c#数据类型

  • Int8 等价于byte
  • Int16 等价于short,占两个字节,-32768~32678
  • Int32 等价于int,占四个字节,-2147483648~2147483647
  • Int64 等价于long,占八个字节,-9223372036854775808 9223372036854775807

BitConverter 类

将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。
BitConverter类包括下表说明了用于从数组的字节,与转换每个基元类型的静态方法。
BitConvert
更多参考MSDN

思路

如何支持int64?目前有几种方式:

  • 底层库全部换成luavm5.3,因为升级可能会有少许兼容性修改。
  • 直接用luavm 5.1.4 编译安卓+iOS底层库,并且打上int64的补丁。
  • int64用字符串代替。(最简单)

c#实现,然后注册到lua虚拟机,在lua中使用

  • LuaUInt64
using System;
using UnityEngine;

public class LuaUInt64
{
    public static byte[] Make(UInt32 high, UInt32 low)
    {
        UInt64 uint64_value = high;
        uint64_value = ((uint64_value << 32) | low);
        return BitConverter.GetBytes(uint64_value);
    }

    public static byte[] FromString(string str)
    {
        UInt64 v;
        UInt64.TryParse(str, out v);
        return BitConverter.GetBytes(v);
    }

    public static byte[] And(byte[] left, byte[] right)
    {
        ulong uint64_value_l = BitConverter.ToUInt64(left, 0);
        ulong uint64_value_r = BitConverter.ToUInt64(right, 0);

        return BitConverter.GetBytes(uint64_value_l & uint64_value_r);
    }

    public static byte[] Or(byte[] left, byte[] right)
    {
        ulong uint64_value_l = BitConverter.ToUInt64(left, 0);
        ulong uint64_value_r = BitConverter.ToUInt64(right, 0);

        return BitConverter.GetBytes(uint64_value_l | uint64_value_r);
    }

    public static byte[] Xor(byte[] left, byte[] right)
    {
        ulong uint64_value_l = BitConverter.ToUInt64(left, 0);
        ulong uint64_value_r = BitConverter.ToUInt64(right, 0);

        return BitConverter.GetBytes(uint64_value_l ^ uint64_value_r);
    }

    public static byte[] FromDouble(double v)
    {
        return BitConverter.GetBytes((ulong)v);
    }

    public static double ToDouble(byte[] v)
    {
        return (double)BitConverter.ToUInt64(v, 0);
    }

    public static string ToString(byte[] v)
    {
        ulong uint64_value = BitConverter.ToUInt64(v, 0);
        return uint64_value.ToString();
    }

    public static byte[] UInt64ToBytes(UInt64 v)
    {
        return BitConverter.GetBytes(v);
    }

    public static UInt64 BytesToUInt64(byte[] v)
    {
        return BitConverter.ToUInt64(v, 0);
    }
}
  • LuaInt64
using System;
using UnityEngine;

public class LuaInt64
{
    public static byte[] Make(Int32 high, Int32 low)
    {
        unchecked
        {
            UInt64 uint64_value = (UInt64)high;
            uint64_value = ((uint64_value << 32) | (UInt32)low);
            return BitConverter.GetBytes(uint64_value);
        }
    }

    public static byte[] FromString(string str)
    {
        Int64 v;
        Int64.TryParse(str, out v);
        return BitConverter.GetBytes(v);
    }

    public static byte[] And(byte[] left, byte[] right)
    {
        long int64_value_l = BitConverter.ToInt64(left, 0);
        long int64_value_r = BitConverter.ToInt64(right, 0);

        return BitConverter.GetBytes(int64_value_l & int64_value_r);
    }

    public static byte[] Or(byte[] left, byte[] right)
    {
        long int64_value_l = BitConverter.ToInt64(left, 0);
        long int64_value_r = BitConverter.ToInt64(right, 0);

        return BitConverter.GetBytes(int64_value_l | int64_value_r);
    }

    public static byte[] Xor(byte[] left, byte[] right)
    {
        long int64_value_l = BitConverter.ToInt64(left, 0);
        long int64_value_r = BitConverter.ToInt64(right, 0);

        return BitConverter.GetBytes(int64_value_l ^ int64_value_r);
    }

    public static byte[] FromDouble(double v)
    {
        return BitConverter.GetBytes((long)v);
    }

    public static double ToDouble(byte[] v)
    {
        return (double)BitConverter.ToInt64(v, 0);
    }

    public static string ToString(byte[] v)
    {
        long int64_value = BitConverter.ToInt64(v, 0);
        return int64_value.ToString();
    }

    public static byte[] Int64ToBytes(Int64 v)
    {
        return BitConverter.GetBytes(v);
    }

    public static Int64 BytesToInt64(byte[] v)
    {
        return BitConverter.ToInt64(v, 0);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值