为什么C#中没有位结构?
就算只值得这点,这是一个完整的比特结构,包含了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
}
它被称为布尔值。至少,它可以提供基本的功能,对吧?在C#中,你不太经常调整二进制位(至少我没有),如果需要的话,你可以使用内置的操作。
有一个BitArray类。
你想用它做什么?请记住,CLR不会试图将多个变量打包成一个字节,因此只有一个变量自己并没有比布尔值更有用。如果你想要一组它们-好吧,就像David指出的那样,这就是BitArray的用途。
如果我们确实有一种比特结构,我认为人们会期望多个比特变量在内存中被高效地打包——通过一开始就不拥有这种类型,我们避免了那种期望,引导人们走向其他解决方案,如比特数组。
如果您拥有一组位标志,那么使用带有标志属性的枚举(enums)和整数可以起到很大的作用。
尽管可能有一些例外,但计算机并不是设计或意图操纵或分配个别比特。 即使在最低的级别(汇编语言或纯机器语言),您也无法分配或访问单个位。在这方面,您拥有与任何编程级别相同的工具:字节和按位操作。
除了已经提到的BitArray类外,还有更高效的BitVector32结构。
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.
请记住,您只能使用32个值。
Dotnetpearls.com上使用BitVector32的示例
你现在可以在C# 7.0中做到这一点!
公共常量int One = 0b0001;
https://learn.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7#numeric-literal-syntax-improvements
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding