mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Make bit-twiddling code more readable.
This commit is contained in:
@@ -241,9 +241,12 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
static boolean areAddressesInSameNetwork(byte[] localIp, byte[] remoteIp,
|
static boolean areAddressesInSameNetwork(byte[] localIp, byte[] remoteIp,
|
||||||
int prefixLength) {
|
int prefixLength) {
|
||||||
if (localIp.length != remoteIp.length) return false;
|
if (localIp.length != remoteIp.length) return false;
|
||||||
|
// Compare the first prefixLength bits of the addresses
|
||||||
for (int i = 0; i < prefixLength; i++) {
|
for (int i = 0; i < prefixLength; i++) {
|
||||||
int mask = 128 >> (i & 7);
|
int byteIndex = i >> 3;
|
||||||
if ((localIp[i >> 3] & mask) != (remoteIp[i >> 3] & mask)) {
|
int bitIndex = i & 7; // 0 to 7
|
||||||
|
int mask = 128 >> bitIndex; // Select the bit at bitIndex
|
||||||
|
if ((localIp[byteIndex] & mask) != (remoteIp[byteIndex] & mask)) {
|
||||||
return false; // Addresses differ at bit i
|
return false; // Addresses differ at bit i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user