I know about boolean logic. However, this question pertains to a specific use case of JS. A &
is used with the same variable on both sides.
In the code snippet below, what is the reason for hash = hash & hash
?
const hash =(string) => {
//set variable hash as 0
var hash = 0;
// if the length of the string is 0, return 0
if (string.length === 0) return hash;
for (i = 0 ;i<string.length ; i++)
{
ch = string.charCodeAt(i);
hash = ((hash << 5) - hash) + ch;
hash = hash & hash;
}
return hash;
};