C# 如何从字节返回位

C# 如何从字节返回位,c#,C#,似乎很简单,但我不知道如何从字节中获取每一位。感谢您的帮助使用 Convert.ToString (value, 2) 使用位移位 例如,第3位:b=(值>>3)和1 最后一个和第1位。 如果需要布尔值,只需将上面的(=)值与值1进行比较。位从右到左“编号”为0到7。因此,要获得第5位,您需要使用byte&(1) 编辑:这将在IF语句中起作用,但如果您只想要1或0,请使用winwaed的解决方案。尝试使用 正如RyuuGan已经发布的,您应该使用。您只需通过使用所需元素调用构造函数将数据放入

似乎很简单,但我不知道如何从字节中获取每一位。感谢您的帮助

使用

 Convert.ToString (value, 2)
使用位移位

例如,第3位:b=(值>>3)和1

最后一个和第1位。 如果需要布尔值,只需将上面的(=)值与值1进行比较。

位从右到左“编号”为0到7。因此,要获得第5位,您需要使用
byte&(1)

编辑:这将在
IF
语句中起作用,但如果您只想要1或0,请使用winwaed的解决方案。

尝试使用


正如RyuuGan已经发布的,您应该使用。您只需通过使用所需元素调用构造函数将数据放入其中

byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
BitArray bitArray = new BitArray( myBytes );
之后,该实例具有一些有趣的属性,可以轻松访问每个位。首先,您可以调用索引运算符来获取或设置每个位的状态:

bool bit = bitArray[4];
bitArray[2] = true;
您还可以通过使用foreach循环(或任何您喜欢的LINQ东西)枚举所有位

foreach(bitArray.Cast()中的变量位)
{
控制台。写入(位+“”);
}
从位返回到某个特定类型(例如int)有点棘手,但使用这种扩展方法非常容易:

public static class Extensions
{
    public static IList<TResult> GetBitsAs<TResult>(this BitArray bits) where TResult : struct
    {
        return GetBitsAs<TResult>(bits, 0);
    }

    /// <summary>
    /// Gets the bits from an BitArray as an IList combined to the given type.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="bits">An array of bit values, which are represented as Booleans.</param>
    /// <param name="index">The zero-based index in array at which copying begins.</param>
    /// <returns>An read-only IList containing all bits combined to the given type.</returns>
    public static IList<TResult> GetBitsAs<TResult>(this BitArray bits, int index) where TResult : struct
    {
        var instance = default(TResult);
        var type = instance.GetType();
        int sizeOfType = Marshal.SizeOf(type);

        int arraySize = (int)Math.Ceiling(((bits.Count - index) / 8.0) / sizeOfType);
        var array = new TResult[arraySize];

        bits.CopyTo(array, index);

        return array;
    }
}
公共静态类扩展
{
公共静态IList GetBitsAs(此位数组位),其中TResult:struct
{
返回GetBitsAs(位,0);
}
/// 
///从位数组中获取位,作为组合到给定类型的IList。
/// 
///结果的类型。
///以布尔值表示的位值数组。
///数组中从零开始复制的索引。
///一种只读IList,包含与给定类型组合的所有位。
公共静态IList GetBitsAs(此位数组位,int索引),其中TResult:struct
{
var实例=默认值(TResult);
var type=instance.GetType();
int-sizeOfType=Marshal.SizeOf(类型);
int arraySize=(int)数学上限((bits.Count-index)/8.0)/sizeOfType);
var array=new TResult[arraySize];
bits.CopyTo(数组、索引);
返回数组;
}
}
有了这一点,您就可以用这一行代码摆脱它:

IList<int> result = bitArray.GetBitsAs<int>();
IList result=bitArray.GetBitsAs();

位3为零引用。即,最低值位为位0。对于一个字节,最高值为7
public static class Extensions
{
    public static IList<TResult> GetBitsAs<TResult>(this BitArray bits) where TResult : struct
    {
        return GetBitsAs<TResult>(bits, 0);
    }

    /// <summary>
    /// Gets the bits from an BitArray as an IList combined to the given type.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="bits">An array of bit values, which are represented as Booleans.</param>
    /// <param name="index">The zero-based index in array at which copying begins.</param>
    /// <returns>An read-only IList containing all bits combined to the given type.</returns>
    public static IList<TResult> GetBitsAs<TResult>(this BitArray bits, int index) where TResult : struct
    {
        var instance = default(TResult);
        var type = instance.GetType();
        int sizeOfType = Marshal.SizeOf(type);

        int arraySize = (int)Math.Ceiling(((bits.Count - index) / 8.0) / sizeOfType);
        var array = new TResult[arraySize];

        bits.CopyTo(array, index);

        return array;
    }
}
IList<int> result = bitArray.GetBitsAs<int>();