java 6中的二进制值

Ser*_*giu 2 java binary type-conversion java-6

我有这个二进制值:

11001001111001010010010010011010.
Run Code Online (Sandbox Code Playgroud)

在java 7中,如果我声明:

int value = 0b11001001111001010010010010011010;
Run Code Online (Sandbox Code Playgroud)

并打印我得到的值-907729766.

我怎样才能在Java 6中实现这一点?

Lui*_*oza 6

你必须首先解析它long,然后将其缩小到int.

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

打印:

-907729766
Run Code Online (Sandbox Code Playgroud)