Unsigned short in Java

2020-01-26 06:37发布

How can I declare an unsigned short value in Java?

15条回答
爷的心禁止访问
2楼-- · 2020-01-26 07:38

From DataInputStream.java

public final int readUnsignedShort() throws IOException {
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
        throw new EOFException();
    return (ch1 << 8) + (ch2 << 0);
}
查看更多
孤傲高冷的网名
3楼-- · 2020-01-26 07:40

Java does not have unsigned types. What do you need it for?

Java does have the 'byte' data type, however.

查看更多
▲ chillily
4楼-- · 2020-01-26 07:42

Yep no such thing if you want to use the value in code vs. bit operations.

查看更多
登录 后发表回答