Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zybnet.utils;
- /**
- * Store a boolean[] in an int
- * Max length for input array is 24, since 8 bit are used to store
- * the length of the source array
- *
- * @author Raffaele
- */
- public class BoolArrayToInt {
- /*
- * 0x000000ff is used to store the source length
- * 0xff000000 is array[0..7]
- * 0x00ff0000 is array[8..15]
- * 0x0000ff00 is array[16..23]
- */
- public static int toInt(boolean[] array) {
- int encoded = 0;
- if (array.length > 24)
- throw new IllegalArgumentException("Source length can't exceed 24: " + array.length + " given");
- int bit = 0x80000000;
- for (boolean b : array) {
- if (b)
- encoded |= bit;
- bit = bit >>> 1;
- }
- encoded |= array.length;
- return encoded;
- }
- public static boolean[] toBoolean(int transport) {
- int length = 0xff & transport;
- boolean[] array = new boolean[length];
- int bit = 0x80000000;
- for (int i = 0; i < length; i++) {
- boolean b = bit == (bit & transport);
- array[i] = b;
- bit = bit >>> 1;
- }
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement