Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Possible Bug?

11 views
Skip to first unread message

Peter Ammon

unread,
Sep 6, 2000, 1:38:17 AM9/6/00
to
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)

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

Patricia Shanahan

unread,
Sep 6, 2000, 2:05:23 AM9/6/00
to

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

-2147483648
-2147483648


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

Peter Ammon

unread,
Sep 6, 2000, 2:20:37 AM9/6/00
to

Thanks for your quick response, 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.

-Peter

Rob Barris

unread,
Sep 6, 2000, 3:39:10 AM9/6/00
to

> 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

Peter Ammon

unread,
Sep 6, 2000, 4:50:13 AM9/6/00
to

My mistake.

In any case, there's still a bug.

-Peter

Adam Charrett

unread,
Sep 6, 2000, 5:52:54 AM9/6/00
to
I have just tried Patricia code on my machine running JDK1.3 and WindowNT 4
and get the same result,
ie
-2147483648
-2147483648
There must be something wrong with your JVM.

Adam

"Peter Ammon" <pa...@cornell.edu> wrote in message
news:39B5E235...@cornell.edu...

Patricia Shanahan

unread,
Sep 6, 2000, 9:15:59 AM9/6/00
to

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

Eric Albert

unread,
Sep 6, 2000, 11:33:00 AM9/6/00
to

> 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/

Steve Chapel

unread,
Sep 6, 2000, 11:44:21 AM9/6/00
to
"Eric Albert" <ejal...@stanford.edu> wrote in message
news:ejalbert-B45AF4...@nntp.stanford.edu...


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/


0 new messages