C# 仿java按位操作的BitSet实现

测试代码

using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BitSet bitset = new BitSet(8);
            bitset.Set(4, true);
            Debug.WriteLine(bitset.ToString());
            Debug.WriteLine(bitset.ToInt());
        }
    }
}

输出

00010000
16

BitSet实现类

using System;
using System.Text;

namespace Test
{
    class BitSet
    {
        public int count;
        private bool[] bitset;

        public BitSet(int count)
        {
            this.count = count;
            bitset = new bool[count];
        }

        public bool this[int index]
        {
            get
            {
                CheckIndexLegal(index);
                return bitset[index];
            }
            set
            {
                CheckIndexLegal(index);
                bitset[index] = value;
            }
        }

        public int Size()
        {
            return count;
        }

        public BitSet Clone()
        {
            BitSet bit = new BitSet(count);
            for (int index = 0; index < count; index++)
            {
                bit[index] = this[index];
            }
            return bit;
        }

        public void Set(int index, bool value = true)
        {
            CheckIndexLegal(index);
            this[index] = value;
        }

        public void Set(int startIndex, int endIndex, bool value = true)
        {
            CheckIndexLegal(startIndex);
            CheckIndexLegal(endIndex);
            for (int index = startIndex; index < endIndex; index++)
            {
                this[index] = value;
            }
        }

        public void Clear()
        {
            for (int index = 0; index < count; index++)
            {
                this[index] = false;
            }
        }

        public void Clear(int index)
        {
            CheckIndexLegal(index);
            this[index] = false;
        }

        public void Clear(int startIndex, int endIndex)
        {
            CheckIndexLegal(startIndex);
            CheckIndexLegal(endIndex);
            for (int index = startIndex; index < endIndex; index++)
            {
                this[index] = false;
            }
        }

        public bool Equals(BitSet bit)
        {
            if (this.count == bit.count)
            {
                for (int index = 0; index < this.count; index++)
                {
                    if (this[index] != bit[index])
                    {
                        return false;
                    }
                }
                return true;
            }

            return false;
        }

        public bool Get(int index)
        {
            CheckIndexLegal(index);
            return this[index];
        }

        public BitSet Get(int startIndex, int endIndex)
        {
            CheckIndexLegal(startIndex);
            CheckIndexLegal(endIndex);
            var length = endIndex - startIndex;
            BitSet bit = new BitSet(length);
            for (int index = 0; index < length; index++)
            {
                bit[index] = this[index + startIndex];
            }
            return bit;
        }

        public bool IsEmpty()
        {
            for (int index = 0; index < count; index++)
            {
                if (this[index])
                {
                    return false;
                }
            }
            return true;
        }

        private void CheckIndexLegal(int index)
        {
            if (index < 0 || index >= count)
            {
                throw new IndexOutOfRangeException();
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            for (int index = count - 1; index >= 0; index--)
            {
                builder.Append((this[index] ? 1 : 0));
            }
            return builder.ToString();
        }

        public int ToInt()
        {
            return Convert.ToInt16(this.ToString(), 2);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值