I have few fields in C++ defined and using those fields we are making a new value. I need to do similar things in Java by following C++ method.
- "text" is defined as uint8_t
- "client_id" is defined as uint16_t
- "proc_id" is defined as uint8_t
- "pod_count" is defined as uint32_t
Below are C++ methods:
typedef uint64_t ClientData;
// convert all four fields into one variable "client_data"
void pack(uint8_t text,
uint16_t client_id,
uint8_t proc_id,
uint32_t pod_count,
ClientData& client_data)
{
client_data = (uint64_t(text) << 56)
+ (uint64_t(client_id) << 40)
+ (uint64_t(proc_id) << 32)
+ pod_count;
}
// now retrieve individual fields value from converted "client_data" variable
void unpack(ClientData client_data,
uint8_t& text,
uint16_t& client_id,
uint8_t& proc_id,
uint32_t& pod_count)
{
pod_count = client_data & 0xffffffff;
client_data = client_data >> 32;
proc_id = client_data & 0xff;
client_data = client_data >> 8;
client_id = client_data & 0xffff;
client_data = client_data >> 16;
text = client_data & 0xff;
}
Now this is what I am doing in Java:
public final class ClientData {
private final byte text;
private final short clientId;
private final byte procId;
private final int podCount;
// this is my pack method corresponding to c++ method above
public long pack() {
return (((long) text) << 56) | (((long) clientId) << 40) | (((long) procId) << 32)
| ((long) podCount);
}
// this is my unpack in Java.
public unpack(long packedValue) {
this.text = (byte) ((packedValue >>> 56) & ((1 << 8) - 1));
this.clientId = (short) ((packedValue >>> 40) & ((1 << 16) - 1));
this.procId = (byte) ((packedValue >>> 32) & ((1 << 8) - 1));
this.podCount = (int) ((packedValue) & ((1L << 32) - 1));
}
}
Now my question is - Whether the logic that I have in pack and unpack method in Java is correct as per above C++ code? And pod_count in C++ is defined as uint32_t but in my above Java code I have it as int.