parseUnsignedInt And divideUnsigned Arithmetic
Treat 32-bit patterns above Integer.MAX_VALUE as large positives with parseUnsignedInt and divideUnsigned, matching what wire formats and hashes encode.
Code
public class UnsignedProtocol {
public static void main(String[] args) {
// 协议字段 4294967295 超出 signed int 上限;parseUnsignedInt 按
// 无符号口径解析,同一组比特在 signed 视角里就是 -1
int maxUnsigned = Integer.parseUnsignedInt("4294967295");
System.out.println("unsigned value: " + Integer.toUnsignedLong(maxUnsigned));
System.out.println("signed view: " + maxUnsigned);
// 除法口径不同:signed 把 -1 当最小负数(-1/2 截断为 0),
// unsigned 把它当最大正数 4294967295 / 2 = 2147483647
System.out.println("signed divide: " + (maxUnsigned / 2));
System.out.println("unsigned divide: " + Integer.divideUnsigned(maxUnsigned, 2));
// 余数与比较同样按无符号次序
System.out.println("unsigned remainder: " + Integer.remainderUnsigned(maxUnsigned, 2));
System.out.println("compareUnsigned vs MIN_VALUE: "
+ Integer.compareUnsigned(maxUnsigned, Integer.MIN_VALUE));
// 超出无符号 32 位范围的字面量会抛异常
try {
Integer.parseUnsignedInt("4294967296");
} catch (NumberFormatException e) {
System.out.println("parse overflow rejected");
}
}
}How the snippet works
parseUnsignedInt And divideUnsigned Arithmetic reduces to the class UnsignedProtocol and the function main.
What to watch for
- Nothing arrives from a registry; the language ships every API used.
Using it
Every line the run emits is tagged — unsigned value:, signed view:, signed divide: — when the file runs.
Key statements
int maxUnsigned = Integer.parseUnsignedInt("4294967295");
Integer.parseUnsignedInt("4294967296");
Interactive Tools for This Topic
FAQ
- What does parseUnsignedInt And divideUnsigned Arithmetic print when run?
- Running the file directly is enough — expect labeled output lines such as `unsigned value:`, `signed view:`, `signed divide:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.