人民医院前端

util.js 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  2. export function int2char(n) {
  3. return BI_RM.charAt(n);
  4. }
  5. //#region BIT_OPERATIONS
  6. // (public) this & a
  7. export function op_and(x, y) {
  8. return x & y;
  9. }
  10. // (public) this | a
  11. export function op_or(x, y) {
  12. return x | y;
  13. }
  14. // (public) this ^ a
  15. export function op_xor(x, y) {
  16. return x ^ y;
  17. }
  18. // (public) this & ~a
  19. export function op_andnot(x, y) {
  20. return x & ~y;
  21. }
  22. // return index of lowest 1-bit in x, x < 2^31
  23. export function lbit(x) {
  24. if (x == 0) {
  25. return -1;
  26. }
  27. var r = 0;
  28. if ((x & 0xffff) == 0) {
  29. x >>= 16;
  30. r += 16;
  31. }
  32. if ((x & 0xff) == 0) {
  33. x >>= 8;
  34. r += 8;
  35. }
  36. if ((x & 0xf) == 0) {
  37. x >>= 4;
  38. r += 4;
  39. }
  40. if ((x & 3) == 0) {
  41. x >>= 2;
  42. r += 2;
  43. }
  44. if ((x & 1) == 0) {
  45. ++r;
  46. }
  47. return r;
  48. }
  49. // return number of 1 bits in x
  50. export function cbit(x) {
  51. var r = 0;
  52. while (x != 0) {
  53. x &= x - 1;
  54. ++r;
  55. }
  56. return r;
  57. }
  58. //#endregion BIT_OPERATIONS