I've been considering the optimization of two of my latest data storage techniques in Java, and would like to know which is really the most memory-efficient. Below are descriptions of the two classes. For the sake of argument, assume they have the same methods for interfacing with the data, which allow the user to get or set the state of any of the bits individually or by range via the following methods:
public boolean getBitState(byte bitIndex)
- Detects and returns the state of the bit at index
bitIndex
- Detects and returns the state of the bit at index
public Clazz setBitState(byte bitIndex, boolean newState)
- Sets the state of the bit at index
bitIndex
tonewState
and returns the resulting object
- Sets the state of the bit at index
public int getStateOfBits(byte startIndex, byte endIndex)
- Detects and returns the state of all bits between
startIndex
andendIndex
as anint
- Detects and returns the state of all bits between
public Clazz setStateOfBits(byte startIndex, byte endIndex, int newState)
- Sets the state of all bits between
startIndex
andendIndex
to the value provided innewState
. - If
newState
has fewer bits than fit, it is made to fit by adding zeros to the left - If
newState
has more bits than fit, the excess bits (on the left side) are cropped
- Sets the state of all bits between
These are the classes I have made to utilize this interface:
IntArray
This class uses an int
as a way of storing 32 bits of data through bitwise functions.
Array32
This class uses an array of 32 boolean
s as its way of storing 32 bits of data through standard array interactions.