English 中文(简体)
防止拜特·马什克签署
原标题:Preventing Sign Extension with Byte Mask

I ve been reading the book TCP/IP Sockets in Java, 2nd Edition. I was hoping to get more clarity on something, but since the book s website doesn t having a forum or anything, I thought I d ask here. In several places, the book uses a byte mask to avoid sign extension. Here s an example:

private final static int BYTEMASK = 0xFF; //8 bits

public static long decodeIntBigEndian(byte[] val, int offset, int size) {
    long rtn = 0;
    for(int i = 0; i < size; i++) {
        rtn = (rtn << Byte.SIZE) | ((long) val[offset + i] & BYTEMASK);
    }
    return rtn;
}

So here s my guess of what s going on. Let me know if I m right. BYTEMASK in binary should look like 00000000 00000000 00000000 11111111. To make things easy, let s just say the val byte array only contains 1 short so the offset is 0. So let s set the byte array to val[0] = 11111111, val[1] = 00001111. At i = 0, rtn is all 0 s so rtn << Byte.SIZE just keeps the value the same. Then there s (long)val[0] making it 8 bytes with all 1 s due to sign extension. But when you use & BYTEMASK, it sets all those extra 1 s to 0 s, leaving that last byte all 1 s. Then you get rtn | val[0] which basically flips on any 1 s in the last byte of rtn. For i = 1, (rtn << Byte.SIZE) pushes the least-significant byte over and leaves all 0 s in place. Then (long)val[1] makes a long with all zero s plus 00001111 for the least-significant byte which is what we want. So using & BYTEMASK doesn t change it. Then when rtn | val[1] is used, it flips rtn s least-significant byte to all 1 s. The final return value is now rtn = 00000000 00000000 00000000 00000000 00000000 00000000 11111111 11111111. So, I hope this wasn t too long, and it was understandable. I just want to know if the way I m thinking about this is correct, and not just completely wacked out logic. Also, one thing that confuses me is the BYTEMASK is 0xFF. In binary, this would be 11111111 11111111, so if it s being implicitly cast to an int, wouldn t it actually be 11111111 11111111 11111111 11111111 due to sign-extension? If that s the case, then it doesn t make sense to me how BYTEMASK would even work. Thank you for reading.

最佳回答

除最后一点外,一切都正确:

<代码>0xFF现为int>(0x000FF),因此,它赢得标记。 总的来说,.html#jls-3.10.1”在 Java的字面编号int,除非其结尾处有L或l,然后是long

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签