简体   繁体   English

为什么我不能在c#中定义一下?

[英]Why can't I define a bit in c#?

为什么C#中没有一点结构?

For what's worth, here is a full-fledged bit structure, complete with int and bool casting and arithmetic operations. 值得一提的是,这里有一个完整的位结构,包括intbool转换和算术运算。 Probably not perfect, but works fine for me. 可能不完美,但对我来说效果很好。 Enjoy! 请享用!

/// <summary>
/// Represents a single bit that can be implicitly cast to/from and compared
/// with booleans and integers.
/// </summary>
/// <remarks>
/// <para>
/// An instance with a value of one is equal to any non-zero integer and is true,
/// an instance with a value of zero is equal to the integer zero and is false.
/// </para>
/// <para>
/// Arithmetic and logical AND, OR and NOT, as well as arithmetic XOR, are supported.
/// </para>
/// </remarks>
public struct Bit
{
    /// <summary>
    /// Creates a new instance with the specified value.
    /// </summary>
    /// <param name="value"></param>
    public Bit(int value) : this()
    {
        Value = value == 0 ? 0 : 1;
    }

    /// <summary>
    /// Gets the value of the bit, 0 or 1.
    /// </summary>
    public int Value { get; private set; }

    #region Implicit conversions

    public static implicit operator Bit(int value)
    {
        return new Bit(value);
    }

    public static implicit operator int(Bit value)
    {
        return value.Value;
    }

    public static implicit operator bool(Bit value)
    {
        return value.Value == 1;
    }

    public static implicit operator Bit(bool value)
    {
        return new Bit(value ? 1 : 0);
    }

    #endregion

    #region Arithmetic operators

    public static Bit operator |(Bit value1, Bit value2)
    {
        return value1.Value | value2.Value;
    }

    public static Bit operator &(Bit value1, Bit value2)
    {
        return value1.Value & value2.Value;
    }

    public static Bit operator ^(Bit value1, Bit value2)
    {
        return value1.Value ^ value2.Value;
    }

    public static Bit operator ~(Bit value)
    {
        return new Bit(value.Value ^ 1);
    }

    public static Bit operator !(Bit value)
    {
        return ~value;
    }

    #endregion

    #region The true and false operators

    public static bool operator true(Bit value)
    {
        return value.Value == 1;
    }

    public static bool operator false(Bit value)
    {
        return value.Value == 0;
    }

    #endregion

    #region Comparison operators

    public static bool operator ==(Bit bitValue, int intValue)
    {
        return 
          (bitValue.Value == 0 && intValue == 0) || 
          (bitValue.Value == 1 && intValue != 0);
    }

    public static bool operator !=(Bit bitValue, int intValue)
    {
        return !(bitValue == intValue);
    }

    public override bool Equals(object obj)
    {
        if(obj is int)
            return this == (int)obj;
        else
            return base.Equals(obj);
    }

    #endregion
}

It is called a boolean. 它被称为布尔值。 At least, it would serve the basic function, right? 至少,它会起到基本作用,对吧? You don't twiddle bits that often in C# (at least, I don't), and if you need to you can use the built in operations. 你不会经常在C#中旋转比特(至少,我没有),如果你需要,你可以使用内置的操作。

一个BitArray类..

What would you want to do with it? 你想用它做什么? Bear in mind that the CLR isn't going to try to pack multiple variables into a byte, so having one on its own would be no more useful than boolean. 请记住,CLR不会尝试将多个变量打包到一个字节中,因此单独使用一个变量并不比布尔值更有用。 If you wanted to have a collection of them - well, that's what BitArray is for, as David pointed out. 如果你想拥有它们的集合 - 嗯,正如大卫指出的那样,这就是BitArray的用途。

If we did have a Bit structure, I suspect people would expect multiple Bit variables to be packed efficiently in memory - by not having the type in the first place, we avoid that expectation and lead people towards other solutions such as BitArray. 如果我们确实有Bit结构,我怀疑人们会期望在内存中有效地打包多个Bit变量 - 由于首先没有类型,我们避免了这种期望并引导人们采用其他解决方案,如BitArray。

如果你有一个位标志的集合,那么使用枚举(使用falgs属性)和整数工作很长。

Though maybe there are rare exceptions, computers are not designed or intended to manipulate or allocate individual bits. 虽然可能有极少数例外,但计算机并非设计或打算操纵或分配单个位。 Even at the lowest levels (assembly or pure machine language), you will not be able to allocate or access an individual bit. 即使在最低级别(汇编语言或纯机器语言),您也无法分配或访问单个位。 You have the same tools available in this regard as you have from any programming level: bytes and bitwise operations. 在这方面,您可以使用与任何编程级别相同的工具:字节和按位操作。

Along with the BitArray class already mentioned there is a also the more efficient BitVector32 Structure . 除了已经提到的BitArray类之外,还有一个更高效的BitVector32结构

BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. 对于布尔值和内部使用的小整数,BitVector32比BitArray更有效。 A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. BitArray可以根据需要无限增长,但它具有类实例所需的内存和性能开销。 In contrast, a BitVector32 uses only 32 bits. 相反,BitVector32仅使用32位。

Keep in mind you are limited to 32 values. 请记住,您只能使用32个值。

Examples of BitVector32 usage at Dotnetpearls.com Dotnetpearls.com上BitVector32使用示例

You can do this now in C# 7.0! 您现在可以在C#7.0中执行此操作!

public const int One = 0b0001; public const int One = 0b0001;

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#numeric-literal-syntax-improvements https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#numeric-literal-syntax-improvements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM