我有一个包含 6 个字节的字节数组,最后 2 个表示端口号,同时正在寻找一种方法将这些最后一个字节转换为一个端口号我遇到过这个片段,
int port = 0;
port |= peerList[i+4] & 0xFF;
port <<= 8;
port |= peerList[i+5] & 0xFF;
它可以工作,但我需要澄清一下它是如何工作的?
最佳答案
int port = 0; // Start with zero
port |= peerList[i+4] & 0xFF; // Assign first byte to port using bitwise or.
port <<= 8; // Shift the bits left by 8 (so the byte from before is on the correct position)
port |= peerList[i+5] & 0xFF; // Assign the second, LSB, byte to port.
关于java - 片段说明字节数组到端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1021463/