I am confused about how a bit vector would work to do this (not too familiar with bit vectors). Here is the code given. Could someone please walk me through this?
Reading Ivan's answer above really helped me, although I would phrase it somewhat differently.
The <<
in (1 << val)
is a bit shifting operator. It takes 1
(which in binary is represented as 000000001
, with as many preceding zeroes as you like / are allocated by memory) and shifts it to the left by val
spaces. Since we're assuming a-z only and subtracting a
each time, each letter will have a value of 0-25, which will be that letter's index from the right in the checker
integer's boolean representation, since we will move the 1
to the left in checker
val
times.
At the end of each check, we see the |=
operator. This merges two binary numbers, replacing all 0
's with 1
's if a 1
exists in either operand at that index. Here, that means that wherever a 1
exists in (1 << val)
, that 1
will be copied over into checker
, while all of checker
's existing 1's will be preserved.
As you can probably guess, a 1
functions here as a boolean flag for true. When we check to see if a character is already represented in the string, we compare checker
, which at this point is essentially an array of boolean flags (1
values) at the indexes of characters that have already been represented, with what is essentially an array of boolean values with a 1
flag at the index of the current character.
The &
operator accomplishes this check. Similar to the |=
, the &
operator will copy over a 1
only if both operands have a 1
at that index. So, essentially, only flags already present in checker
that are also represented in (1 << val)
will be copied over. In this case, that means only if the current character has already been represented will there be a 1
present anywhere in the result of checker & (1 << val)
. And if a 1
is present anywhere in the result of that operation, then the value of the returned boolean is > 0
, and the method returns false.
This is, I'm guessing, why bit vectors are also called bit arrays. Because, even though they aren't of the array data type, they can be used similar to the way arrays are used in order to store boolean flags.