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.