Binary numbers

Hi, I am still learning java, and this engine, and I am currently working on a test-game for it.
But while writing some data for the game, I hit a snag. The game is a maze, and I construct it with tiles, and each tile has boolean-values for its four walls (on/off).

As I was planning how to format the code for a test-level, I though of using binary numbers to represent the bools,
[java]byte wallBools = 0b0111;[/java]
but then I get this:
“binary literals are not supported in -source 1.5 (use -source 7 or higher to enable binary literals)”

Why is binary not supported?
And is there something I can do to use binary?

Well as stated it was not supported in that java version, so you are left with defining it in hex unless you update to a newer jvm.

To define a var from binary its easiest to use the hex representation for that binary number.

int mask = 0x1; // Represents 0000…0001 (32 bits in total)
int mask = 0xF1; represents 0000…11110001

Each hex value represents 4 bits.

If you insist to use byte, you can use only 2 hex digits.

byte wallBools = 0xFF; (All bits 1)

Or as in your example

byte wallBools = 0x07; // (00000111)

Thank you for the answer.
However defining it in hex defies the point of writing it in number.
I want to do it that way to allow for a quick edit, by the change of a simple number.

As I have already pointed out, I am still fairly new to java, how do I update to a newer “jvm” to use with jMonkeyEngine SDK?

There are various ways of going around to do that.

For now if you just want to test stuff, you can use

int value = Integer.parseInt(“0111”, 2);
Even though this may not be a good way of doing it, it works.

The most common practise is to set certain bits rather than using the binary literal.
For changing certain numbers in the variable you will use bit operations.
See examples below for how to toggle/set/unset a certain bit in an integer

[java]
public void toggleBit(int variable, int position) {
variable ^= 1 << position;
}

public void setBit(int variable, int position) {
	variable |= 1 &lt;&lt; position;
}

public void unsetBit(int variable, int position) {
	variable &amp;= ~(1 &lt;&lt; position);
}

[/java]

Edit: the methods above is not isolidated, but heres how I use it in my game for input management using a single integer for all input (character control)

[java]
input = (pressed ? input | (1 << pi.getBitPos()) : input
& ~(1 << pi.getBitPos()));
[/java]

Erm, don’t use bitfields anyway. Use EnumSet().

It’s provides all the speed of bitfields but all the hard work is done for you.

http://dhruba.name/2008/12/31/effective-java-item-32-use-enumset-instead-of-bit-fields/

As far as I understand, both your methods require me to use strings, I want to avoid that, because a huge manually-written map filled with individual strings is highly likely to introduce lots of typing-errors.

I assume JVM stands for Java Virtual Machine, and that’s what I need to update to use binary, right?

In that case, I will repeat my last question:
How do I update to a newer “jvm” to use with jMonkeyEngine SDK?

@Grambo said: As far as I understand, both your methods require me to use strings, I want to avoid that, because a huge manually-written map filled with individual strings is highly likely to introduce lots of typing-errors.

I assume JVM stands for Java Virtual Machine, and that’s what I need to update to use binary, right?

In that case, I will repeat my last question:
How do I update to a newer “jvm” to use with jMonkeyEngine SDK?

Well from what you explained in the first post, I’m assuming you were not trying to actually make a large grid based on bits,
as for your case its probably wise to use EnumSet as zarch describes.
Enums and enumsets are cheap resource wise and would work exceptionally for your usecase.
I dont see why you would want to use bits for what you are trying to do, nor do I see why you would need the binary literal accessible.

So you create an enum class called something like Wall.
[java]
public enum Wall {
NORTH, SOUTH, EAST, WEST
}
[/java]

Then in your tile class you make an enumset which contains the directions you wish to block.

[java]
public class Tile {

private EnumSet walls = EnumSet.of(Wall.NORTH, Wall.SOUTH, Wall.EAST, Wall.WEST);


public boolean isNorthBlocked() {
        return walls.contains(Wall.NORTH);
}

}
[/java]

You just have to go to Oracle web site and download the JDK 7. Install it, and that should be ok.
Beware of MacOS-X. I remember seeing posts saying JME does not work on JDK7 / MacOS-X

Edit : You also have to set your source compatibility level to 7 in your project (inside JME, but for each project).

1 Like
@yang71 said: You just have to go to Oracle web site and download the JDK 7. Install it, and that should be ok. Beware of MacOS-X. I remember seeing posts saying JME does not work on JDK7 / MacOS-X

Edit : You also have to set your source compatibility level to 7 in your project (inside JME, but for each project).

Sorry to your others, but I most here say:
FINALLY ONE THAT ACTUALLY GIVES THE ANSWER TO WHAT I ASK FOR!

-turns out, all i needed to do was what I marked in BOLD

-Good alternatives though.

Sometimes we try to be helpful beyond what you really asked because we perceive trouble you will run into later. If it’s just for a test level then maybe it’s ok to painfully encode everything in binary by hand.

…otherwise, you get a similar visual representation from:
[java]
byte[][] map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
[/java]
And it’s easier to reference the individual values, you can make it any size, etc…

…if I understood what you are trying to do.

If you are still hot to encode it as binary then you could still compile it down pretty trivially.

Yes, i am thankful for the extra help you give. But I DO have experience with programming.
I worked with Actionscript 3 before. What I am trying to make is a re-make of a testgame I made in FLASH, an improved version that is…

A 2D-array map like what you (pspeed) posted there, is just what I am gonna make, except there will be 4 digits, and they will be in binary.

Knowing flash != knowing java.

EnumSets is the correct way to do it in java.

Do it badly if you want though. Not my problem to fix later…

@zarch said: Knowing flash != knowing java.

EnumSets is the correct way to do it in java.

Do it badly if you want though. Not my problem to fix later…

I’m with you on the fact that he’s doing it the hard way but I still fail to see how enum sets would achieve his goal of visible seeing an array of 1s and 0s.

Zarch, a little correction here

Knowing flash != Programming (FLASH is more used for animations than games)
Knowing Actionscript 3 (the language developed for flash) != Knowing java

Knowing Actionscript 3 -> Knowing programming <- Knowing Java

Its true that I don't automatically understand Java with AS3, but I DO understand programming.