Bitwise Math

Started by lede701 on Tue, 09/15/2020 - 02:44

Topic category: Help with modding (Java Edition)

Last seen on 22:44, 23. Oct 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Bitwise Math
Tue, 09/15/2020 - 02:44

Not sure how to ask this question it is kind of complicated so please be patient as I try and explain one of my favorite data manipulation techniques.  I've used this technique in many personally created game engines and other professional programming tasks I've ran into over the years.

Using a Bitwise operator on an Integer has helped me store directional information in a very compact format.  Since processors don't look at numbers the way we do we can use the Binary data in an integer to help speed directional evaluations.  For example take these "Magic Numbers" 1, 2, 4, 8, 16, and 32.  This would use 6 bits in a 32 or 64 bit Integer:

Real Number Binary Version Direction
1 0000 0001 North
2 0000 0010 South
4 0000 0100 East
8 0000 1000 West
16 0001 0000 Up
32 0010 0000 Down

 

From this chart you can see how using Binary could be advantageous since instead of tracking 6 directions in six variables we could store all this in one variable.  Yes I know this is only 8 bits but a 32 and 64 bit number would just be padded with 0.  Anyways back to my problem in MCreator.  When I try and store this in a number since all numbers are decimal the Java compile complains that something like if(3 & MCreatorDemoModVariable.WorldVariable.get(world).TEST_DIRECTION_1 == NORTH) you'll get the error message "bad operator types for binary operator &".  In the code editor it would look something like this:

Bitwise flow example

This is kind of expected since Java doesn't like to perform Bitwise operators on decimal numbers.  Is there a way to tell the GUI programming to typecast the variable for this kind of bitwise operation?

I know we can just go modify the source code and fix this issue but once you open the modified procedure in the GUI code editor all those fixes will be wiped out.  If you have any ideas please share I would appreciate it.

Last seen on 08:52, 27. Oct 2020
Joined Nov 2019
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
unfortunately our…
Wed, 09/16/2020 - 00:27

unfortunately our magnificent mcreator creator doesn't like integers very much.

you can try requesting a feature that adds more types of local variables like integers, blockstates or entities...

Last seen on 22:44, 23. Oct 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Thanks, I was very afraid of…
Wed, 09/16/2020 - 03:21

Thanks, I was very afraid of that being the answer.  Definitely a limitation of a GUI based programming IDE connecting to a well defined language like Java.  Time to crack open a real IDE I guess.