Why can't I define a little in C #?

Why is there no little structure in C #?

+8
source share
8 answers

What is a full-fledged bit structure for, complete with int and bool casting and arithmetic operations. Probably not perfect, but works great for me. Enjoy it!

 /// <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 } 
+19
source share

It is called a boolean. At least that would serve the core function, right? You haven't interleaved bits that are often in C # (at least I don't), and if you need to, you can use the built-in operations.

+16
source share

There is a BitArray class ..

+15
source share

What would you like to do about it? Keep in mind that the CLR is not going to try to pack several variables into bytes, so having one alone will not be more useful than a boolean. If you want to have their collection - well, that means BitArray , as David pointed out.

If we had a bit structure, I suspect that people expect several bit variables to be effectively packed into memory - not having this type in the first place, we avoid this expectation and bring people to other solutions, such as BitArray.

+9
source share

If you have a set of bit flags, then using enumerations (with the falgs attribute) and integers is a long way to go.

+3
source share

Although there may be rare exceptions, computers are not intended or not designed to manipulate or extract individual bits. Even at the lowest levels (assembly or pure machine language), you cannot select or access a single bit. In this regard, you have the same tools as any level of programming: bytes and bitwise operations.

+2
source share

Along with the already mentioned BitArray class, there is also a more efficient BitVector32 structure .

BitVector32 is more efficient than BitArray for Boolean values ​​and small integers that are used internally. BitArray can grow indefinitely as needed, but it has the memory and performance that an instance of the class requires. In contrast, BitVector32 uses only 32 bits.

Remember that you are limited to 32 values.

Examples of using BitVector32 at Dotnetpearls.com

+2
source share
0
source share

All Articles