3

Possible Duplicate:
In Java, can I define an integer constant in binary format?

In python, you can do something like:

a = 0b00000010 which would set a to 2.

Is it possible to do something like that in Java? I know I could just go through and assign my varibles by the number instead of binary, but I like the visual.

Thanks ~Aedon

9
  • You can do the same thing in java 7 a = 0b00000010 Commented Jul 21, 2011 at 17:00
  • 1
    this is a dup question. answered here: stackoverflow.com/questions/867365/… Commented Jul 21, 2011 at 17:00
  • 2
    You could wait for Java7, that supports binary literals (for some inexplicable reason) Commented Jul 21, 2011 at 17:01
  • 2
    @skaffman: It's inexplicable that Java would support binary literals? Is there a more self-documenting way of showing int masks in code? Commented Jul 21, 2011 at 17:02
  • 1
    @ratchet freak - Nothing is wrong with it. But I am doing bitmask flags and wanted to visualize how the flags were laid out. Preference is in question here not functionality. Commented Jul 21, 2011 at 17:18

1 Answer 1

9

In Java 7, you can do

int a = 0b00000010;

However if you're working with an older version, I'm afraid you're stuck with

int a = Integer.parseInt("00000010", 2);
Sign up to request clarification or add additional context in comments.

1 Comment

or you can write in hex using 0x int i = 0x10; which is equal to int i = 16;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.