小编gor*_*non的帖子

Bitwise op意外地变为负面

有人可以向我解释为什么我得到这些结果?

public static int ipv4ToInt(String address) {
    int result = 0;

    // iterate over each octet
    for(String part : address.split(Pattern.quote("."))) {

        // shift the previously parsed bits over by 1 byte
        result = result << 8;

        System.out.printf("shift = %d\n", result);

        // set the low order bits to the current octet
        result |= Integer.parseInt(part);

        System.out.printf("result = %d\n", result);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

对于ipv4ToInt("10.35.41.134"),我得到:

shift = 0
result = 10
shift = 2560
result = 2595
shift = 664320
result = 664361 …

java bit-manipulation

3
推荐指数
1
解决办法
101
查看次数

标签 统计

bit-manipulation ×1

java ×1