If I use a single int to represent an ascii character set, how is using it reducing the storage space by a factor of 8 ? as compared to an array of 256 boolean values ? The single int is also functioning like a bit vector.
A boolean in java will occupy 1 bit as it can represent only true or false values. So for example if i have an array of boolean values. boolean[] char_set = new boolean[256] This will occupy 256 bits correct ? I’m reading that if i use a single int like a bit vector which means I can use 32 bits to cover 256 values. I guess that is reduction by a factor of 8. But why does the code below work ?
It is checking if there are any duplicates in the string. They are assuming an ascii char set. Str is some string.
int checker = 0;
for(int i=0;i<str.length();i++)
{
int val = str.charAt(i) - 'a';
if(checker& (1<<val)) > 0)
{
return false;
}
checker |= (1<<val);
}
return true;
}
Can someone particularly explain how the bit vector logic works in this case. They are assuming the string contains lowercase characters.
An
intis 32 bits, not 256 bits. It alone would not be enough to represent a set of 256 possible items. You need 8 of them. I am not sure what you mean that you can use just 32 bits then.It’s not clear what you’re looping over — what is
str? All 256 values from 0 to 255? I’m suspicious because you are subtracting'a'. Is your universe of values only 32 possible chars? Then sure you can use 32 bits. But where did 256 ever come from then?Your mask condition needs to be
!= 0to work for the highest bit set.(A
boolean‘s “real” size is opaque to the Java programmer. In reality, you will find that it is not 1 bit (machines aren’t bit-addressable), nor even 1 byte. Java actually uses a whole 32-bit word. But this is not really related to your question.)