简体   繁体   中英

A possibly silly question about “custom” integers in C#

Good afternoon,

This may sound like a silly question, but it would be really useful if there was a way around this... Is there any way I can get custom bit-depth integers (for example, a 20-bit integer) in C#?

Thank you very much.

Build a struct that takes a 32 bit integer and bit masks it with
0000 0000 0000 1111 1111 1111 1111 1111 , or ( 0x08FF )
before storing it in an internal private field.

   public struct TwentyBitInt
   {
      private const int mask = 0x08FF;
      private int val;
      private bool isDef;


      private TwentyBitInt(int value)
      {
         val = value & mask;
         isDef = true;
      }
      public static TwentyBitInt Make(int value) 
      { return new TwentyBitInt(value); }

      public int Value { get { return val; } }
      public bool HasValue { get { return isDef; } }

      public static TwentyBitInt Null = new TwentyBitInt();

      public static explicit operator int (TwentyBitInt twentyBit)
      { 
          if (!HasValue) throw new ArgumentNullValueException(); 
          return twentyBit.val;
      }
      public static implicit operator TwentyBitInt (int integerValue)
      { return Make(integerValue); }

      // etc.
    }

You can also appropriately overload the arithmetic operators so that arithmetic operations behave consistently with the business rules for the domain they are to be used in..

I think you can but it is not going to be easy. You can use something like a BitArray to create an arbitrary length bitstring. Then you have to write all the code yourself to treat it like an integer. That's the hard part.

Though you can't create a structure that would fit exactly "20 bits" in memory, you could create your own structure that will "behave like" a 20 bit data type (by storing a private "regular" data type, and using bitwise operators in your public setter to keep only the wanted bits).

struct TwoBits
{
    int m_data;

    public int Data
    {
        set
        {
            m_data = 0x03 & value;
        }
        get
        {
            return m_data;
        }
    }
}

Good question. As most things, the answer will depend on what you need to do with it.

I did something similar years ago maintaining a school research project. We had a custom integer type, though it was not necessarily defined by bit-depth per se. It was a prime-factor (with remainder) representation. This was very handy for what it was supposed to do, which was multiply and divide very large numbers. It was very poor for addition and subtraction.

There are also many other "custom" number implementations out there, a very common one is a custom Fixed Point representation (often seen when implementing deterministic physical simulations in games). There are probably some good overflow posts about this subject. These would be a good start for ideas on how to abstract and then implement a custom numeric representation.

If you are set on variable-bit-depth integer representation, consider wrapping an internal representation. One possibility may be to use an IEnumerable<byte> and extend it byte by byte as its magnitude increases. Write operators that work against a dynamic range. This may not necessarily be the most optimal form of representation or most performant, but it would likely be the easiest.

Another possible solution may be to do something similar, but with 'ulong'. Not as space efficient, but we benefit from 64-bit operations.

Just spitballing :)

Hope this helps!

Perhaps check out operator overloading and design a custom class (probably more work than you want to do though). http://www.c-sharpcorner.com/UploadFile/prasadh/OperatorOverloading11142005003229AM/OperatorOverloading.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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