binary.dart icon indicating copy to clipboard operation
binary.dart copied to clipboard

[Feature] Provide bit constants

Open tekert opened this issue 3 years ago • 0 comments

Hi, i needed to do some bitmasking at compile time where possible and it would be great if the library provided constant for bits, for example:

class CompatibleBits
{
  /// The largest possbile unsigned integer that is valid in JavaScript.
  static const MAXJSUINT = ALLBITSSET53;
  static const ALLBITSSET53 = 0x1fffffffffffff;

  // Javascript shift operations works on 32 bit values only.
  static const int bit1 = MAXJSUINT & 1 << 0;
  static const int bit2 = MAXJSUINT & 1 << 1;
  static const int bit3 = MAXJSUINT & 1 << 2;
  static const int bit4 = MAXJSUINT & 1 << 3;
  static const int bit5 = MAXJSUINT & 1 << 4;
  static const int bit6 = MAXJSUINT & 1 << 5;
  static const int bit7 = MAXJSUINT & 1 << 6;
  static const int bit8 = MAXJSUINT & 1 << 7;
  static const int bit9 = MAXJSUINT & 1 << 8;
  static const int bit10 = MAXJSUINT & 1 << 9;
  static const int bit11 = MAXJSUINT & 1 << 10;
  static const int bit12 = MAXJSUINT & 1 << 11;
  static const int bit13 = MAXJSUINT & 1 << 12;
  static const int bit14 = MAXJSUINT & 1 << 13;
  static const int bit15 = MAXJSUINT & 1 << 14;
  static const int bit16 = MAXJSUINT & 1 << 15;
  static const int bit17 = MAXJSUINT & 1 << 16;
  static const int bit18 = MAXJSUINT & 1 << 17;
  static const int bit19 = MAXJSUINT & 1 << 18;
  static const int bit20 = MAXJSUINT & 1 << 19;
  static const int bit21 = MAXJSUINT & 1 << 20;
  static const int bit22 = MAXJSUINT & 1 << 21;
  static const int bit23 = MAXJSUINT & 1 << 22;
  static const int bit24 = MAXJSUINT & 1 << 23;
  static const int bit25 = MAXJSUINT & 1 << 24;
  static const int bit26 = MAXJSUINT & 1 << 25;
  static const int bit27 = MAXJSUINT & 1 << 26;
  static const int bit28 = MAXJSUINT & 1 << 27;
  static const int bit29 = MAXJSUINT & 1 << 28;
  static const int bit30 = MAXJSUINT & 1 << 29;
  static const int bit31 = MAXJSUINT & 1 << 30;
  static const int bit32 = MAXJSUINT & 1 << 31;

  // We can use exended bits until 53 bits are used.
  // shift is not working past 32 bits in javascript but
  // we cant still use the precision bits.
  static const int bit33 = 0x100000000;
  static const int bit34 = 0x200000000;
  static const int bit35 = 0x400000000;
  static const int bit36 = 0x800000000;
  static const int bit37 = 0x1000000000;
  static const int bit38 = 0x2000000000;
  static const int bit39 = 0x4000000000;
  static const int bit40 = 0x8000000000;
  static const int bit41 = 0x10000000000;
  static const int bit42 = 0x20000000000;
  static const int bit43 = 0x40000000000;
  static const int bit44 = 0x80000000000;
  static const int bit45 = 0x100000000000;
  static const int bit46 = 0x200000000000;
  static const int bit47 = 0x400000000000;
  static const int bit48 = 0x800000000000;
  static const int bit49 = 0x1000000000000;
  static const int bit50 = 0x2000000000000;
  static const int bit51 = 0x4000000000000;
  static const int bit52 = 0x8000000000000;
  static const int bit53 = 0x10000000000000;
}

I then can use these to give a name to options with these constants (sadly dart doesnt support constant getters or functions)

class Options
{
  static const int option1 = CompatibleBits.bit1;
  static const int option2 = CompatibleBits.bit2;

  int getOptionsForThing()
  {
      return option1 | option2;
  }
}

i would be great to have this on the library, without throwing or checks, as performant as possible. Or with enums.

tekert avatar Sep 19 '22 08:09 tekert