Answer the question
In order to leave comments, you need to log in
How does this code work (bitwise shifts)?
In general, there is a code in the JAVA language (the original language is different, but it was ported to JAVA), you need to understand exactly how it works .. The essence is something like this: in order not to use 1 byte each time to transfer a boolean value, it was proposed to use a map of these then subtract them one by one, so 8 boolean values fit into 1 byte (1 bit = 1 boolean). A network packet has approximately the following structure: A map of boolean values, the data itself.
MOMENT NUMBER ONE Modification of positions.
The following code snippet is present in the two methods (getBit/setBit) below. It is clear to me that this is a transformation (shift?) of positions, but I cannot understand why.
int _local2 = (position >> 3);
int _local3 = (7 ^ (position & 7));
boolean getBit(int position) {
*КОПИПАСТ ИЗ МОМЕНТА НОМЕР ОДИН*
*тут ставится позиция буффера в _local2*
return !(((this.map.readByte() & (1 << _local3)) == 0));
}
void setBit(int position, Boolean value) {
*ТУТ ТОЖЕ КОПИПАСТ ИЗ МОМЕНТА НОМЕР ОДИН*
*ТУТ ВЫПОЛНЯЕТСЯ ПРОВЕРКА, ЕСЛИ В БУФФЕРЕ НЕТ МЕСТА ДЛЯ ЗАПИСИ СЛЕДУЮЩЕГО БИТА, ТО АЛЛОЦИРУЕТСЯ 1 БАЙТ*
*тут ставится позиция буффера в _local2*
if (value) {
this.map.writeByte((byte) ((this.map.readByte(_local2) | (1 << _local3))));
} else {
this.map.writeByte((byte) ((this.map.readByte(_local2) & (0xFF ^ (1 << _local3)))));
}
}
private static boolean getBit(byte[] ba, int position) {
int _local2 = (position >> 3);
int _local3 = (7 ^ (position & 7));
byte b = ba[_local2];
return !((b & (1 << _local3)) == 0);
}
private static byte[] setBit(byte[] ba, int position, boolean value) {
int _local2 = (position >> 3);
int _local3 = (7 ^ (position & 7));
if (value) {
ba[_local2] = (byte) (ba[_local2] | (1 << _local3));
} else {
ba[_local2] = (byte) (ba[_local2] & (0xFF ^ (1 << _local3)));
}
return ba;
}
static String toBinary(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
for (int i = 0; i < Byte.SIZE * bytes.length; i++)
sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
return sb.toString();
}
public static void main(String[] args) {
byte[] map = new byte[2];
map = setBit(map, 0, true);
map = setBit(map, 1, false);
map = setBit(map, 2, true);
map = setBit(map, 3, true);
map = setBit(map, 4, true);
map = setBit(map, 14, false);
map = setBit(map, 15, true);
System.out.println(toBinary(map));// 1011100000000001 RIGHT!
System.out.println(getBit(map, 0));// true
System.out.println(getBit(map, 1));// false
System.out.println(getBit(map, 2));// true
System.out.println(getBit(map, 3));// true
System.out.println(getBit(map, 4));// true
System.out.println(getBit(map, 14));// false
System.out.println(getBit(map, 15));// true
BitSet bitSet = new BitSet();
bitSet.set(0, true);
bitSet.set(1, false);
bitSet.set(2, true);
bitSet.set(3, true);
bitSet.set(4, true);
bitSet.set(14, false);
bitSet.set(15, true);
System.out.println(toBinary(bitSet.toByteArray()));//0001110110000000 WTF??
}
Answer the question
In order to leave comments, you need to log in
this.map.writeByte((byte) ((this.map.readByte(_local2) & (0xFF ^ (1 << _local3)))));
read the _local2 byte and compare it with the reverse mask depending on the bit at the _local3 position, then write the resulting byte. How can you figure out what it's for?
Someone will figure it out for you ... seriously?
Take a look and understand: https://en.wikipedia.org/wiki/Bitwise_operation . Moreover, you better understand what is being done there and why in terms of business logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question