I've been trying out the Distributed Code Jam testing tool, and I noticed an 
apparent bug when running Java code due to the use of the "char" type in the 
API. This program sends the char with value 255 from node 0 to node 1, but node 
1 receives the char value 65535:

class Main {
    public static void main(String[] args) {
        if (message.MyNodeId() == 0) {
            char outC = 255;
            System.out.printf("Sending %d%n", (int)outC);
            message.PutChar(1, outC);
            message.Send(1);
        } else if (message.MyNodeId() == 1) {
            message.Receive(0);
            char inC = message.GetChar(0);
            System.out.printf("Received %d%n", (int)inC);
        }
    }
}

Here's what it prints:

$ dcj test --source Main.java --library dummy_library.java --nodes 2
...
STDOUT 0: Sending 255
STDOUT 1: Received 65535
Duration: 87ms (longest running instance: 1)


The trouble happens because "char" in Java is a 16-bit unsigned type, but it's 
being sent as a signed 8-bit signed value (-1 in this case). When it gets 
turned back into a char, sign extension fills in the higher-order bits as 1s, 
so the value received is different from the value sent (even though the value 
sent was in the range 0 to 255).

To avoid this, you could mask off the lower-order bits (e.g. "value & 0xff") so 
that received characters are always in the 0-255 range. Alternatively, since 
the intention of PutChar/GetChar is to send a single byte, it seems like the 
Java "message" class should really be using the "byte" type instead of the 
"char" type for GetChar and PutChar.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/cdf3ec8e-0fe4-438e-8b5f-bc39be761dc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to