A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

90 lines
2.7 KiB

  1. package com.bitshift.parsing.utils;
  2. //This class contains implementations of methods to
  3. // -- pack an integer into 4 consecutive bytes of a byte array
  4. // -- unpack an integer from 4 consecutive bytes of a byte array
  5. // -- exhaustively test the pack and unpack methods.
  6. //
  7. // This file should be saved as PackableMemory.java. Once it has been
  8. // compiled, the tester can be invoked by typing "java PackableMemory"
  9. public class PackableMemory {
  10. int size;
  11. public byte mem[] = null;
  12. public PackableMemory(int size)
  13. {
  14. this.size = size;
  15. this.mem = new byte[size];
  16. }
  17. // Pack the 4-byte integer val into the four bytes mem[loc]...mem[loc+3].
  18. // The most significant porion of the integer is stored in mem[loc].
  19. // Bytes are masked out of the integer and stored in the array, working
  20. // from right(least significant) to left (most significant).
  21. public void pack(int val, int loc)
  22. {
  23. final int MASK = 0xff;
  24. for (int i = 3; i >= 0; i--)
  25. {
  26. mem[loc+i] = (byte)(val & MASK);
  27. val = val >> 8;
  28. }
  29. }
  30. // Unpack the four bytes mem[loc]...mem[loc+3] into a 4-byte integer,
  31. // and return the resulting integer value.
  32. // The most significant porion of the integer is stored in mem[loc].
  33. // Bytes are 'OR'ed into the integer, working from left (most significant)
  34. // to right (least significant)
  35. public int unpack(int loc)
  36. {
  37. final int MASK = 0xff;
  38. int v = (int)mem[loc] & MASK;
  39. for (int i = 1; i < 4; i++)
  40. {
  41. v = v << 8;
  42. v = v | ((int)mem[loc+i] & MASK);
  43. }
  44. return v;
  45. }
  46. // Test the above pack and unpack methods by iterating the following
  47. // over all possible 4-byte integers: pack the integer,
  48. // then unpack it, and then verify that the unpacked integer equals the
  49. // original integer. It tests all nonnegative numbers in ascending order
  50. // and then all negative numbers in ascending order. The transition from
  51. // positive to negative numbers happens implicitly due to integer overflow.
  52. public void packTest()
  53. {
  54. int i = 0;
  55. long k = 0;
  56. do
  57. {
  58. this.pack(i,4);
  59. int j = this.unpack(4);
  60. if (j != i)
  61. {
  62. System.out.printf("pack/unpack test failed: i = %d, j = %d\n",i,j);
  63. System.exit(0);
  64. }
  65. i++; k++;
  66. }
  67. while (i != 0);
  68. System.out.printf("pack/unpack test successful, %d iterations\n",k);
  69. }
  70. // main routine to test the PackableMemory class by running the
  71. // packTest() method.
  72. public static void main(String[] args)
  73. {
  74. PackableMemory pm = new PackableMemory(100);
  75. pm.packTest();
  76. System.exit(0);
  77. }
  78. }