Parsing bitwise operations from a string in Java for Box2D

This is a short post which might be helpful to some people who are using Box2D. If you want to filter collisions in Box2D you need to use mask bits. In order to manipulate mask bits you need to use Java bitwise operations. For a project I'm working on I need to store these mask bits and bitwise operations in a text file. The information then needs to be parsed by Java to create my mask bit. I need to parse strings in the form: ~0xFFFF&0x0001. Java doesn't seem to be able to parse this kind of string natively so I wrote a simple function which does the job:

  1. /**
  2. * Parses bitwise operations from a string.
  3. * @param s
  4. * @return
  5. * @throws Exception
  6. */
  7. public static int parseBitwiseOperators (String s) throws Exception {
  8. // Define a list of all bitwise operations
  9. List<String> operators = new ArrayList<String>();
  10. operators.add("|");
  11. operators.add("&");
  12. operators.add("~");
  13. operators.add("^");
  14. operators.add("<<");
  15. operators.add(">>");
  16. operators.add(">>>");
  17.  
  18. // Array to store operations
  19. List<String> ops = new ArrayList<String>();
  20. // Array to store integers
  21. List<String> ints = new ArrayList<String>();
  22.  
  23. int start = 0;
  24.  
  25. // Loop over the string and check if each character is an
  26. // operation
  27. for(int i=0; i<s.length(); i++) {
  28. String c = s.substring(i, i+1);
  29.  
  30. if(c.equals(""))
  31. continue;
  32.  
  33. // Check for single character operators
  34. if(operators.contains(c)) {
  35. ops.add(c);
  36. // for case of starting with a ~ command there is no preceding
  37. // string
  38. if(i>0)
  39. ints.add(s.substring(start, i));
  40.  
  41. start = i+1;
  42. i = start;
  43. continue;
  44. }
  45.  
  46. // for case of three character command i.e. >>>>
  47. if(i+3 < s.length()) {
  48. String c3 = s.substring(i, i+3);
  49. if(operators.contains(c3)) {
  50. ops.add(c3);
  51. ints.add(s.substring(start, i));
  52. start = i+3;
  53. i = start;
  54. continue;
  55. }
  56. }
  57.  
  58. // for case of two character command i.e. >>
  59. if(i+2 < s.length()) {
  60. String c2 = s.substring(i, i+2);
  61. if(operators.contains(c2)) {
  62. ops.add(c2);
  63. ints.add(s.substring(start, i));
  64. start = i+2;
  65. i = start;
  66. continue;
  67. }
  68. }
  69.  
  70.  
  71. }
  72. // Add the int after the final command
  73. ints.add(s.substring(start, s.length()));
  74.  
  75. for(String s1: ops) {
  76. System.out.print(s1+"\n");
  77. }
  78. for(String s1: ints) {
  79. System.out.print(s1+"\n");
  80. }
  81.  
  82. if(ints.size()==0) {
  83. throw new Exception("String contains no valid integers");
  84. }
  85.  
  86.  
  87. int i;
  88. // If the first command is a ~
  89. if(ops.get(0).equals("~") && ops.size()==ints.size()) {
  90. i = ~ Integer.decode(ints.get(0));
  91. ops = ops.subList(1, ops.size());
  92. }
  93. else {
  94. i = Integer.decode(ints.get(0));
  95. }
  96.  
  97. // Create the chain of bitwise operations
  98. for(int j=0; j<ops.size(); j++) {
  99. if(ops.get(j).equals("|")) {
  100. i = i | Integer.decode(ints.get(j+1));
  101. }
  102. if(ops.get(j).equals("&")) {
  103. i = i & Integer.decode(ints.get(j+1));
  104. }
  105. if(ops.get(j).equals("^")) {
  106. i = i ^ Integer.decode(ints.get(j+1));
  107. }
  108. if(ops.get(j).equals("~")) {
  109. i = i ^ Integer.decode(ints.get(j+1));
  110. }
  111. if(ops.get(j).equals("<<")) {
  112. i = i << Integer.decode(ints.get(j+1));
  113. }
  114. if(ops.get(j).equals(">>")) {
  115. i = i >> Integer.decode(ints.get(j+1));
  116. }
  117. if(ops.get(j).equals(">>>")) {
  118. i = i >>> Integer.decode(ints.get(j+1));
  119. }
  120. }
  121.  
  122. return i;
  123. }

Here's the function. I'm not going to go into any great explanation because I assume if you are trying to do this then you know what you're doing. Hope this if helpful to someone!

Tweet: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.