Bits manipulation expert, please join :)

Hi,

(still on the subject of my space shooter).
I have an idea to represent my movement patterns, wich seem fast, and flexible using scripting and bits manipulations.
But, I suck at bits manipulations, maybe some of you can help.

Here is the basic idea:

  • The pattern script is a binary file.
  • Each “instruction set” is a 64 bits word of 4 parts of 8 bits.
  • The first 8 bits represent the “repeat” count (how many time the instruction must be repeated before going to the next one).
  • The second 8 bits represent the instruction itself (SHOOT, MOVE, SHIELD, RETREAT, TARGET, etc…)
  • The third and fourth 8 bits represent X, and Y respectively for a MOVE instruction, or any other parameters if needed by the instruction.

ex:
00000001 00000001 00000001 00000001
REPEAT 1 MOVE X=1 Y=1
00000001 00000001 00000000 00000001
REPEAT 1 MOVE X=0 Y=1

Would end up making my entitie move one step down/right and the one step down.

Questions:
How the hell do I do that in java? (and what about negative numbers) :slight_smile:
(both creating the “step” and reading the step)
It would be even better if multiple instruction could be use of the same “line” (like MOVE & SHOOT) without creating a new instruction.

I would appreciated an example, or links to documentation around that.

Thanks,

Seb

4 x 8 is 32 not 64… I would learn that before trying to make a game :).

I’m not sure what the problem would be… the only catch is to use >>> instead of >> to avoid problems with sign extension.

Since bytes are signed values you just need to pull out the data into a byte and if you had 11111111 in X or Y it woudl be treated as -1.


//int blah = 00000001 00000001 00000001 11111111
int blah = 0x010101ff;
int repeat = blah >>> 24;
int move = (blah >> 16) & 0xffff;
byte dx = (byte) (blah >> 8);
byte dy = (byte blah;

Hi again,

I tried to figure out your code, but first I don’t understand the 0x010101ff; notation.
And also the mask.

I did manage to make the following code sorta work, but as soon as my value is negative everything thing is messed out.
(I tried to understand the signed/unsigned shifting stuff without luck).
Hey, I guess that’s the way it is for my kind of developper, the one that never really learn low level stuff like bits…

Have a nice day,

Seb


int brepeat = 255;
int bmove = 1;
byte bx = 1;
byte by = -1;

int blah = (brepeat << 24) | (bmove << 16) | (bx << 8) | (by);
            
int repeat = (blah >> 24) & 0xff;
int move = (blah >> 16) & 0xff;
byte dx = (byte)(blah >> 8);
byte dy = (byte)blah;
            
System.out.println( ""+blah );
System.out.println( ""+repeat );
System.out.println( ""+move );
System.out.println( ""+dx );
System.out.println( ""+dy );

You don’t need bit manipulation to have binary file with bytes. Here is an example with your data structure:


import java.io.*;

public class Binary {
  public static void main(String[] args) {
    try {
      // writing the data to binary file
       DataOutputStream out = new DataOutputStream(new FileOutputStream("binary.dat"));
       int brepeat = 255; 
       int bmove   = 1; 
       byte bx       = 1; 
       byte by       = -1; 

       out.writeByte(brepeat);
       out.writeByte(bmove);
       out.writeByte(bx);
       out.writeByte(by);

       // reading data from binary file
       DataInputStream  in  = new DataInputStream(new FileInputStream("binary.dat"));
    System.out.println("brepeat: "+(in.readByte() & 0xff));
      System.out.println("bmove: "+(in.readByte() & 0xff));
      System.out.println("bx: "+in.readByte());
      System.out.println("by: "+in.readByte());
    }
    catch(Exception e) {
      e.printStackTrace ();
    }
  }
}

Thanks you rulez my world today ;D

I’ll let you know when the shooter come to life.

Seb