C# 自定义数据类型

由于需要一个大于64位的数据类型(ulong),因此需要自己写一个数据类型存储大约600位的数据,并能够按位与  按位或


主要用到的技术:

1.操作符重载operator & ,operator |

2.自定义的强制转换    implicit operator



using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Shujuleixing
{
    class Program
    {
        static void Main(string[] args)
        {
            LongLongLong x = new ulong[] { long.MaxValue, long.MaxValue, long.MaxValue };
            LongLongLong y = new ulong[] { 0, 0, 0 };
            LongLongLong huo = x | y;
            Console.WriteLine(huo);
            LongLongLong yu = x & y;
            Console.WriteLine(yu.ToString());
            Console.Read();
        }
    }


    public struct LongLongLong
    {
        public override string ToString()
        {
            string resultS = "";
            foreach (var uulong in ULongList)
            {
                resultS += Convert.ToString((long)uulong, 2);
            }
            return resultS;
        }
        public List<ulong> ULongList;
        public static LongLongLong operator |(LongLongLong a, LongLongLong b)
        {
            var newLongLongLong = new LongLongLong {ULongList = new List<ulong>()};
            for (int i = 0; i < a.ULongList.Count; i++)
            {
                newLongLongLong.ULongList.Add(a.ULongList[i] | b.ULongList[i]);
            }
            return newLongLongLong;
        }
        public static LongLongLong operator &(LongLongLong a, LongLongLong b)
        {
            var newLongLongLong = new LongLongLong {ULongList = new List<ulong>()};
            for (int i = 0; i < a.ULongList.Count; i++)
            {
                newLongLongLong.ULongList.Add(a.ULongList[i] & b.ULongList[i]);
            }
            return newLongLongLong;
        }
        public static implicit operator LongLongLong(ulong[] oriLong)
        {
            var tempL = new LongLongLong { ULongList = oriLong.ToList() };
            return tempL;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值