Search code examples
javabinarytype-conversionjava-6

Binary value in java 6


I have this binary value :

11001001111001010010010010011010.

In java 7, if I declare:

int value = 0b11001001111001010010010010011010;

and print the value I get -907729766.

How can I achieve this in Java 6 as well?


Solution

  • You have to parse it as a long first, then narrow it into int.

    String s = "11001001111001010010010010011010";
    int i = (int)Long.parseLong(s, 2); //2 = binary
    System.out.println(i);
    

    Prints:

    -907729766