I am working in an int[][][] array and I need to return an address of one field of that array from a static function.
Given the fact that the dimensions of the Array will stay small (int[32][32][32]) I had the idea to return one number containing all three Values, instead of using an Array containing the three numbers.
I already had a working solution, where i packed my number into a String and unpacked it in the receiving method via Integer.parseInt(String).
Unfortunately this didn't work quite, well in terms of runtime, so i thought of bitshifting.
I apologize for my bad english and hope this simple question is worth your time :)
If your numbers are in the range 0...255, this example will encode three numbers into one int and then decode it again...
public class BitDemo {
public static void main(String[] args) {
int encoded = encode(20, 255, 10);
int[] decoded = decode(encoded);
System.out.println(Arrays.toString(decoded));
}
private static int[] decode(int encoded) {
return new int[] {
encoded & 0xFF,
(encoded >> 8) & 0xFF,
(encoded >> 16) & 0xFF
};
}
private static int encode(int b1, int b2, int b3) {
return (b1 & 0xFF) | ((b2 & 0xFF) << 8) | ((b3 & 0xFF) << 16);
}
}
(b1 & 0xFF) - Gets the first 8 bits of b1
((b2 & 0xFF) << 8) - Gets the first 8 bits of b2 and shifts them left 8 bits
((b3 & 0xFF) << 16) - Gets the first 8 bits of b3 and shifts them left 16 bits
These three numbers are ORed together.
If you have negative numbers or number more than 255, you'll get different results.
Given N, M and O and assuming N * M * O doesn't overflow, you can pack and unpack your indices like this:
int packed = o * (N * M) + m * N + n;
int o = packed / (N * M);
int m = (packed % (N * M)) / N;
int n = packed % N; // is equal to (packed % (N * M)) % N
If you want to use bit-shifting, make sure you chose N, M, O as powers of 2. Let's say N = 2^NS, M = 2^MS and O = 2^OS packing and unpacking would look like this:
int packed = (o << (NS + MS)) | (m << NS) | n;
int o = (packed >> (NS + MS)) & ((1 << OS) - 1);
int m = (packed >> NS) & ((1 << MS) - 1);
int n = packed & ((1 << NS) - 1);
All of the above assume n=0..N-1, m=0..M-1 and o=0..O-1.
来源:https://stackoverflow.com/questions/37305266/how-do-i-save-multiple-small-integers-in-one-integer-via-bitshifting