BitArray替代的.NET Micro Framework的 [英] BitArray alternative for the .NET Micro Framework

查看:166
本文介绍了BitArray替代的.NET Micro Framework的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有BitArray替代的.NET Micro Framework的? 我想简单地用一个布尔[],但你怎么把它转换回来 成一个byte []?

Is there a BitArray alternative for the .NET Micro Framework? I was thinking about simply using a bool[], but how can you convert it back into a byte[] ?

在完整的框架,考虑到位是一个BitArray,以下工作:

In the full framework, considering "bits" is a BitArray, the following works:

byte[] data = new byte[dimensions / 8];
bits.CopyTo(data, 0);

不过,我似乎无法找到BitArray类在微架构

But I cannot seem to find the BitArray class in the micro framework

推荐答案

这是不是非常难以复制 BitArray 的功能。首先,如果你需要少于65位,那么你可以用或更小的做到这一点。

It's not terribly difficult to duplicate the functionality of BitArray. First, if you need fewer than 65 bits, then you can do it with a long or smaller.

要设置单个位:

void Set(ref long ba, int bit)
{
    ba |= 1L << bit;
}

要清除位:

void Clear(ref long ba, int bit)
{
    long mask = 1L << bit;
    mask = ~mask;
    ba &= mask;
}

要查看是否有位设置:

bool IsSet(long ba, int bit)
{
    long mask = 1L << bit;
    return (ba & mask) != 0;
}

如果你有超过64位,那么你需要创建一个数组(字节[] ,可能),并做了分工,以确定哪些字节/位要修改。上面的方法将工作,只要你改变字节

If you have more than 64 bits, then you'll need to create an array (byte[], probably), and do the division to determine which byte/bit you want to modify. The methods above will work, provided you change the long to byte.

例如,如果您有:

byte[] myBytes = new byte[128];

您有1024位。

要设置一下:

void Set (int bit)
{
    int byte = bit/8;
    int bitIndex = bit%8;
    myBytes[byte] |= (byte)(1 << bitIndex);
}

其他方法使用相同的数学获取字节和位索引,和设置,清算和测试位是一样的用上面的例子。

这篇关于BitArray替代的.NET Micro Framework的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆