为什么我不能在 c# 中定义一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/352089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I define a bit in c#?
提问by ScalaMyCapela
Why isn't there a bit structure in C#?
为什么 C# 中没有一点结构?
回答by Ed S.
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# 中乱搞(至少,我不会),如果你需要,你可以使用内置的操作。
回答by VVS
If you have a collection of bit flags, then using enums (with falgs attribute) and integers work a long way.
如果您有一组位标志,那么使用枚举(带有 falgs 属性)和整数会有很长的路要走。
回答by Jon Skeet
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. If you wanted to have a collection of them - well, that's what BitArrayis for, as David pointed out.
你想用它做什么?请记住,CLR 不会尝试将多个变量打包到一个字节中,因此单独使用一个变量不会比布尔值更有用。如果您想拥有它们的集合 - 好吧,正如大卫指出的那样,这就是BitArray的用途。
If we didhave 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。
回答by Dave Cousineau
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.
尽管可能存在极少数例外,但计算机并非旨在或旨在操纵或分配单个位。即使在最低级别(汇编或纯机器语言),您也无法分配或访问单个位。在这方面,您可以使用与任何编程级别相同的工具:字节和按位操作。
回答by Chris Weber
Along with the BitArray class already mentioned there is a also the more efficient BitVector32 Structure.
除了已经提到的 BitArray 类,还有一个更高效的BitVector32 Structure。
BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32 uses only 32 bits.
对于内部使用的布尔值和小整数,BitVector32 比 BitArray 更有效。BitArray 可以根据需要无限增长,但它具有类实例所需的内存和性能开销。相比之下,BitVector32 仅使用 32 位。
Keep in mind you are limited to 32 values.
请记住,您仅限于 32 个值。
回答by Konamiman
For what's worth, here is a full-fledged bit structure, complete with int
and bool
casting and arithmetic operations. Probably not perfect, but works fine for me. Enjoy!
对于什么样的价值,这里是一个完全成熟的位结构,完整的int
和bool
铸造和算术运算。可能并不完美,但对我来说效果很好。享受!
/// <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
}
回答by LeWoody
You can do this now in C# 7.0!
您现在可以在 C# 7.0 中执行此操作!
public const int One = 0b0001;
公共常量 int 一 = 0b0001;