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

Just as you can define an integer constant in hex or octal, can I do it in binary?

I admit that this is a very simple (and stupid) question. My google searches are empty.

+78
java syntax binary
May 15 '09 at 7:14 a.m.
source share
8 answers

So, with the release of Java SE 7, binary notation comes standard out of the box. The syntax is pretty 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, in particular, when declaring class level variables as binary files, there is no problem when initializing a static variable using binary notation:

 public static final int thingy = 0b0101; 

Just be careful not to overflow numbers with too much data, otherwise you will get a compiler error:

 byte data = 0b1100110011; // Type mismatch: cannot convert from int to byte 

Now, if you really want to get some fantasy, you can combine this other neat new feature in Java 7, known as numeric literals with underscores. Take a look at these fancy binary notation examples with literal underscores:

 int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011; long bow = 0b1__01010101__01010101__01010101__01010111L; 

Now is it not so beautiful and clean, not to mention well readable?

I pulled these code snippets from a short article that I wrote about a topic on TheServerSide. Feel free to check it out for more details:

Java 7 and Binary Notation: Mastering the OMP Java Programmer Exam (OCPJP)

+54
Dec 20 2018-11-12T00:
source share

In Java 7:

 int i = 0b10101010; 

There are no binary literals in older versions of Java (see other answers for alternatives).

+152
Nov 07 '09 at 12:47
source share

There are no binary literals in Java, but I suppose you could do this (although I don't see the point):

 int a = Integer.parseInt("10101010", 2); 
+51
May 15, '09 at 7:18
source share

Reply from Ed Swangren

 public final static long mask12 = Long.parseLong("00000000000000000000100000000000", 2); 

works great. I used long instead of int and added modifiers to clarify the possible use as a bitmask. However, there are two inconveniences with this approach.

  • Direct typing of all these zeros is error prone
  • The result is not available in decimal or hexadecimal format at the time of development

I can offer an 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 right to left); and when you hover over the tooltip

 long YourClassName.mask12 = 4096 [0x1000] 

appears in Eclipse. You can define more complex constants, such as:

 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 maskForSomething variable will still be available in Eclipse during development.

+18
Jul 30 '09 at 3:00
source share

Using binary constants to mask

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 } 
+15
Mar 14 '11 at 12:23
source share

Search for โ€œJava literal syntaxโ€ on Google, and you come up with some entries.

There is octal syntax (the prefix of your number is 0), decimal syntax, and hexadecimal syntax with the prefix "0x". But no syntax for binary notation.

Some examples:

 int i = 0xcafe ; // hexadecimal case int j = 045 ; // octal case int l = 42 ; // decimal case 
+12
May 15 '09 at 7:24 a.m.
source share

If you want to mess with a lot of binaries, you can define some constants:

 public static final int BIT_0 = 0x00000001; public static final int BIT_1 = 0x00000002; 

and etc.

or

 public static final int B_00000001 = 0x00000001; public static final int B_00000010 = 0x00000002; public static final int B_00000100 = 0x00000004; 
+2
Jul 09 '10 at 17:04 on
source share

A slightly more uncomfortable answer:

 public class Main { public static void main(String[] args) { byte b = Byte.parseByte("10", 2); Byte bb = new Byte(b); System.out.println("bb should be 2, value is \"" + bb.intValue() + "\"" ); } } 

which outputs [java] bb should be 2, the value is "2"

0
May 15 '09 at 8:00 a.m.
source share



All Articles