Util.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package util;
  2. import java.math.BigInteger;
  3. import java.nio.ByteBuffer;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import exceptions.LengthNotMatchException;
  7. public class Util {
  8. public static long nextLong(Random r, long range) {
  9. long bits, val;
  10. do {
  11. bits = (r.nextLong() << 1) >>> 1;
  12. val = bits % range;
  13. } while (bits - val + (range - 1) < 0L);
  14. return val;
  15. }
  16. public static long getSubBits(long l, int end, int start) {
  17. if (start < 0)
  18. throw new IllegalArgumentException(start + " < 0");
  19. if (start > end)
  20. throw new IllegalArgumentException(start + " > " + end);
  21. long mask = (1L << (end - start)) - 1L;
  22. return (l >>> start) & mask;
  23. }
  24. public static BigInteger getSubBits(BigInteger bi, int end, int start) {
  25. if (start < 0)
  26. throw new IllegalArgumentException(start + " < 0");
  27. if (start > end)
  28. throw new IllegalArgumentException(start + " > " + end);
  29. BigInteger mask = BigInteger.ONE.shiftLeft(end - start).subtract(BigInteger.ONE);
  30. return bi.shiftRight(start).and(mask);
  31. }
  32. public static long setSubBits(long target, long input, int end, int start) {
  33. input = getSubBits(input, end - start, 0);
  34. long trash = getSubBits(target, end, start);
  35. return ((trash ^ input) << start) ^ target;
  36. }
  37. public static BigInteger setSubBits(BigInteger target, BigInteger input, int end, int start) {
  38. if (input.bitLength() > end - start)
  39. input = getSubBits(input, end - start, 0);
  40. BigInteger trash = getSubBits(target, end, start);
  41. return trash.xor(input).shiftLeft(start).xor(target);
  42. }
  43. public static byte[] rmSignBit(byte[] arr) {
  44. if (arr[0] == 0)
  45. return Arrays.copyOfRange(arr, 1, arr.length);
  46. return arr;
  47. }
  48. // c = a ^ b
  49. public static byte[] xor(byte[] a, byte[] b) {
  50. if (a.length != b.length)
  51. throw new LengthNotMatchException(a.length + " != " + b.length);
  52. byte[] c = new byte[a.length];
  53. for (int i = 0; i < a.length; i++)
  54. c[i] = (byte) (a[i] ^ b[i]);
  55. return c;
  56. }
  57. // a = a ^ b to save memory
  58. public static void setXor(byte[] a, byte[] b) {
  59. if (a.length != b.length)
  60. throw new LengthNotMatchException(a.length + " != " + b.length);
  61. for (int i = 0; i < a.length; i++)
  62. a[i] = (byte) (a[i] ^ b[i]);
  63. }
  64. public static byte[] intToBytes(int i) {
  65. ByteBuffer bb = ByteBuffer.allocate(4);
  66. bb.putInt(i);
  67. return bb.array();
  68. }
  69. public static void debug(String s) {
  70. // only to make Communication.java compile
  71. }
  72. public static void disp(String s) {
  73. // only to make Communication.java compile
  74. }
  75. public static void error(String s) {
  76. // only to make Communication.java compile
  77. }
  78. public static void error(String s, Exception e) {
  79. // only to make Communication.java compile
  80. }
  81. }