Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Similar to how you can define an integer constant in hexadecimal or octal, can I do it in binary?

I admit this is a really easy (and stupid) question. My google searches are coming up empty.

like image 677
Aaron Fi Avatar asked May 15 '09 07:05

Aaron Fi


People also ask

How do you convert int to binary in Java?

toBinaryString(int i) To convert an integer to binary, we can simply call the public static String toBinaryString(int i) method of the Integer class. This method returns the binary string that corresponds to the integer passed as an argument to this function.

How do you represent int in binary?

To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order. Here is an example of such conversion using the integer 12.

How do you declare a binary variable in Java?

A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.


2 Answers

In Java 7:

int i = 0b10101010; 

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

like image 163
Russ Hayward Avatar answered Oct 14 '22 01:10

Russ Hayward


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

like image 33
Cameron McKenzie Avatar answered Oct 14 '22 00:10

Cameron McKenzie