简体   繁体   English

关于C#中“自定义”整数的一个可能愚蠢的问题

[英]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#? 这可能听起来像一个愚蠢的问题,但如果有办法解决这个问题会非常有用......有什么方法可以在C#中获得自定义位深度整数(例如,20位整数)?

Thank you very much. 非常感谢你。

Build a struct that takes a 32 bit integer and bit masks it with 构建一个采用32位整数和位掩码的结构
0000 0000 0000 1111 1111 1111 1111 1111 , or ( 0x08FF ) 0000 0000 0000 1111 1111 1111 1111 1111 ,或( 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. 您可以使用类似BitArray的东西来创建任意长度的位串。 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). 虽然您无法在内存中创建一个完全符合“20位”的结构,但您可以创建自己的结构,其“行为”类似于20位数据类型(通过存储私有的“常规”数据类型,并使用按位公共设置器中的运算符只保留所需的位)。

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. 一种可能性是使用IEnumerable<byte>并在其幅度增加时逐字节地扩展它。 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'. 另一种可能的解决方案可能是做类似的事情,但使用'ulong'。 Not as space efficient, but we benefit from 64-bit operations. 不是节省空间,但我们受益于64位操作。

Just spitballing :) 只是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 http://www.c-sharpcorner.com/UploadFile/prasadh/OperatorOverloading11142005003229AM/OperatorOverloading.aspx

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

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