Similar to how you can define an integer constant in hexadecimal or octal, can I do it in binary?
-
2Just FYI, in pretty much every programming language, the idiom is to write binary constants in hex, and not the actual binary string.abyx– abyx2009-11-07 13:07:25 +00:00Commented Nov 7, 2009 at 13:07
-
3Right, but I imagine that's because hex is the closest thing available, not because there's something wrong with binary. In languages I've used that do support binary literals, I don't think the convention is to ignore this feature.Ken– Ken2010-07-10 21:29:36 +00:00Commented Jul 10, 2010 at 21:29
-
1docs.oracle.com/javase/8/docs/technotes/guides/language/…user3136131– user31361312016-06-16 10:59:43 +00:00Commented Jun 16, 2016 at 10:59
-
Year 2017: the accepted answer must be that provided by Russ. See: stackoverflow.com/a/1692932/716079Lourenco– Lourenco2017-08-15 19:53:53 +00:00Commented Aug 15, 2017 at 19:53
8 Answers
In Java 7:
int i = 0b10101010;
There are no binary literals in older versions of Java (see other answers for alternatives).
4 Comments
_ characters to make the sequence more readable: int i = 0b1010_1010_0011;So, with the release of Java SE 7, binary notation comes standard out of the box. The syntax is quite straight forward and obvious if you have a decent understanding of binary:
byte fourTimesThree = 0b1100;
byte data = 0b0000110011;
short number = 0b111111111111111;
int overflow = 0b10101010101010101010101010101011;
long bow = 0b101010101010101010101010101010111L;
And specifically on the point of declaring class level variables as binaries, there's absolutely no problem initializing a static variable using binary notation either:
public static final int thingy = 0b0101;
Just be careful not to overflow the numbers with too much data, or else you'll get a compiler error:
byte data = 0b1100110011; // Type mismatch: cannot convert from int to byte
Now, if you really want to get fancy, you can combine that other neat new feature in Java 7 known as numeric literals with underscores. Take a look at these fancy examples of binary notation with literal underscores:
int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011;
long bow = 0b1__01010101__01010101__01010101__01010111L;
Now isn't that nice and clean, not to mention highly readable?
I pulled these code snippets from a little article I wrote about the topic over at TheServerSide. Feel free to check it out for more details:
Java 7 and Binary Notation: Mastering the OCP Java Programmer (OCPJP) Exam
3 Comments
There are no binary literals in Java, but I suppose that you could do this (though I don't see the point):
int a = Integer.parseInt("10101010", 2);
7 Comments
The answer from Ed Swangren
public final static long mask12 =
Long.parseLong("00000000000000000000100000000000", 2);
works fine. I used long instead of int and added the modifiers to clarify possible usage as a bit mask. There are, though, two inconveniences with this approach.
- The direct typing of all those zeroes is error prone
- The result is not available in decimal or hex format at the time of development
I can suggest alternative approach
public final static long mask12 = 1L << 12;
This expression makes it obvious that the 12th bit is 1 (the count starts from 0, from the right to the left); and when you hover mouse cursor, the tooltip
long YourClassName.mask12 = 4096 [0x1000]
appears in Eclipse. You can define more complicated constants like:
public final static long maskForSomething = mask12 | mask3 | mask0;
or explicitly
public final static long maskForSomething = (1L<<12)|(1L<<3)|(1L<<0);
The value of the variable maskForSomething will still be available in Eclipse at development time.
1 Comment
Using binary constants to masking
Declare constants:
public static final int FLAG_A = 1 << 0;
public static final int FLAG_B = 1 << 1;
public static final int FLAG_C = 1 << 2;
public static final int FLAG_D = 1 << 3;
and use them
if( (value & ( FLAG_B | FLAG_D )) != 0){
// value has set FLAG_B and FLAG_D
}
Comments
Search for "Java literals syntax" on Google and you come up with some entries.
There is an octal syntax (prefix your number with 0), decimal syntax and hexadecimal syntax with a "0x" prefix. But no syntax for binary notation.
Some examples:
int i = 0xcafe ; // hexadecimal case
int j = 045 ; // octal case
int l = 42 ; // decimal case
2 Comments
byte b = 0b01000001 (for a better readness byte b = 0b0100_0001).If you want to mess around with lots of binary you could define some constants:
public static final int BIT_0 = 0x00000001;
public static final int BIT_1 = 0x00000002;
etc.
or
public static final int B_00000001 = 0x00000001;
public static final int B_00000010 = 0x00000002;
public static final int B_00000100 = 0x00000004;