Im trying to implement a class that stores a 32-bit number without using the int
primitive type. For doing so, I'm using two short
variables msbs
and lsbs
to store the 32 bits of the number, 16 bits in each variable.
The variable msbs
will store the first 16 bits of the number and the lsbs
variable the 16 bits left.
When It comes to save the given bytes to the variables I apply the next formula: (The bytes order are given as Little-Endian notation)
Input -> byte[] n = {0b00110101, -3, 0b1001, 0b0};
to the number 0b00000000 00001001 11111101 00110101 (654645)
msbs = ((n[3] << 8) | n[2]);
lsbs = ((n[1] << 8) | n[0]);
As shown below
private void saveNumber(byte[] n) {
msbs = (byte)((n[3] << 8) | n[2]);
lsbs = (byte)((n[1] << 8) | n[0]);
System.out.println(Integer.toBinaryString((n[1] << 8) | n[0]));//Prints 11111111111111111111110100110101
System.out.println("msbs -> " + Integer.toBinaryString(msbs));
System.out.println("lsbs -> " + Integer.toBinaryString(lsbs));//Prints 110101
}
The line
System.out.println(Integer.toBinaryString((n[1] << 8) | n[0]));//Prints 11111111111111111111110100110101
prints exactly what I need, despite the huge amount of useless 1's bits at the beggining (of which I can get rid of just by casting it to short
)
But when I print the lsbs
where I store the exact same value (apparently) it outputs 110101 when It should be 0b1111110100110101
Why does this behavior occur? I understand It must be something with the "internal" casting performed by Java at the time of store the value 11111111111111111111110100110101
into a 16 bits primitive type (Which personally, I think sholdn't be happening because I am shifting 8 bits to the left in an 8-bits number which should give me a 16-bits number)
As a side note, the msbs
variable is doing exactly what I want it to do, so the problem should be related to the way that Java represents the negative numbers
Btw, I know Java isn't exactly the best language to have fun with bits.