ABitUtils.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. namespace ChocolArm64
  2. {
  3. static class ABitUtils
  4. {
  5. public static int CountBitsSet(long Value)
  6. {
  7. int Count = 0;
  8. for (int Bit = 0; Bit < 64; Bit++)
  9. {
  10. Count += (int)(Value >> Bit) & 1;
  11. }
  12. return Count;
  13. }
  14. public static int HighestBitSet32(int Value)
  15. {
  16. for (int Bit = 31; Bit >= 0; Bit--)
  17. {
  18. if (((Value >> Bit) & 1) != 0)
  19. {
  20. return Bit;
  21. }
  22. }
  23. return -1;
  24. }
  25. public static long Replicate(long Bits, int Size)
  26. {
  27. long Output = 0;
  28. for (int Bit = 0; Bit < 64; Bit += Size)
  29. {
  30. Output |= Bits << Bit;
  31. }
  32. return Output;
  33. }
  34. public static long FillWithOnes(int Bits)
  35. {
  36. return Bits == 64 ? -1L : (1L << Bits) - 1;
  37. }
  38. public static long RotateRight(long Bits, int Shift, int Size)
  39. {
  40. return (Bits >> Shift) | (Bits << (Size - Shift));
  41. }
  42. public static bool IsPow2(int Value)
  43. {
  44. return Value != 0 && (Value & (Value - 1)) == 0;
  45. }
  46. }
  47. }