System.out.println(1 << ((1==1) ? -1 : 1));
returns 0, as expected.
However, the code
int a=1;
System.out.println(1 << ((a==1) ? -1 : 1));
returns -214783648 (which corresponds to all bits set in a signed int)
I'm using an Apple G3/400 with Codewarrior 5 and MRJ 2.2. I had it
confirmed on a Windows 98 machine with JDK 1.3.
-Peter
Peter Ammon wrote:
>
> The code
>
> System.out.println(1 << ((1==1) ? -1 : 1));
>
> returns 0, as expected.
>
> However, the code
>
> int a=1;
> System.out.println(1 << ((a==1) ? -1 : 1));
>
> returns -214783648 (which corresponds to all bits set in a signed int)
Any 2's complement binary integer with all bits set is odd. -214783648
has bit pattern 0x80000000.
While your expectation of 0 as the result of 1 << -1 is very reasonable,
and I wish you were right, it is not how shift is defined in Java.
"If the promoted type of the left-hand operand is int, only the five
lowest-order bits of the right-hand operand are used as the shift
distance. It is as if the right-hand operand were subjected to a bitwise
logical AND operator & (§15.22.1) with the mask value 0x1f. The shift
distance actually used is therefore always in the range 0 to 31,
inclusive."
- JLS "15.19 Shift Operators" at
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
-1 has all bits on, so the result of masking down to five low order bits
is 31. The 1 is then left shifted 31 bits.
>
> I'm using an Apple G3/400 with Codewarrior 5 and MRJ 2.2. I had it
> confirmed on a Windows 98 machine with JDK 1.3.
I just ran the following program:
public class Test {
public static void main (String[] argv) {
System.out.println(1 << ((1==1) ? -1 : 1));
int a=1;
System.out.println(1 << ((a==1) ? -1 : 1));
}
}
on a Windows 98 machine with JDK 1.3, and got
If you get anything else for this program, something is broken. Could
you post a test program that gets 0 for the first result?
Patricia
Thanks for your quick response, Patricia.
The program you posted above outputs
on my machine. The simple program
public class BuggyApp {
public static void main(String args[]) {
System.out.println(1 << ((true) ? -1 : 1));
}
}
outputs 0 as well.
-Peter
> The code
>
> System.out.println(1 << ((1==1) ? -1 : 1));
>
> returns 0, as expected.
>
> However, the code
>
> int a=1;
> System.out.println(1 << ((a==1) ? -1 : 1));
>
> returns -214783648 (which corresponds to all bits set in a signed int)
>
Um, all bits set in a signed int would be 0xFFFFFFFF or -1.
-214783648 (what you typed) is 0xF332A960... (sayeth ProgCalc)
-2147483648 is 0x80000000. (I will guess that is what you meant).
Rob
My mistake.
In any case, there's still a bug.
-Peter
Adam
"Peter Ammon" <pa...@cornell.edu> wrote in message
news:39B5E235...@cornell.edu...
Peter Ammon wrote:
...
> public class BuggyApp {
> public static void main(String args[]) {
> System.out.println(1 << ((true) ? -1 : 1));
> }
> }
>
> outputs 0 as well.
>
> -Peter
-2147483648 on Windows 98, JDK 1.3. I think you have a buggy platform.
My first suspicion would be optimization, either in the compiler or at
run time. Constant folding could be done very locally on the expressions
that get 0.
Patricia
> The program you posted above outputs
>
> 0
> -2147483648
>
> on my machine. The simple program
>
> public class BuggyApp {
> public static void main(String args[]) {
> System.out.println(1 << ((true) ? -1 : 1));
> }
> }
>
> outputs 0 as well.
Run the program again, but before you do, quit all applications that use
Java and remove the MRJ Symantec JITC library from the MRJ Libraries
folder. I suspect that you'll now get the expected result. Then head
to <http://www.apple.com/java/>, repeat the test with MRJ 2.2.2 (you
said that you're running 2.2), and if it fails there, go to
<http://bugreport.apple.com/> and file a bug report (probably against
MRJ's JIT).
-Eric
--
Eric Albert ejal...@stanford.edu
http://www.stanford.edu/~ejalbert/
Before you file a bug report on the JIT, examine the bytecode that compiler
produces (javap -c BuggyApp). It could be that the compiler compiles the
println line as
System.out.println(0);
in which case there's no way you can expect a JIT to print anything but 0!
:-)
--
Java Programmers FAQ: http://www.afu.com/javafaq.html
Java Docs: http://java.sun.com/docs/