I'm trying to understand how bitwise operation works in Javascript especially when used as the condition result.
const number = 19 if(number & 1) { console.log('one') } if(number & 2) { console.log('two') } if(number & 4) { console.log('four') } if(number & 8) { console.log('eight') } if(number & 16) { console.log('sixteen') } // one two sixteen if(number & 1 === 1) { console.log('one') } if(number & 2 === 2) { console.log('two') } if(number & 4 === 4) { console.log('four') } if(number & 8 === 8) { console.log('eight') } if(number & 16 === 16) { console.log('sixteen') } // one two four eight sixteen The first part of the code produces one two sixteen which I expect it to be. But the second part of the code produces one two four eight sixteen. Since number & 4 should be 0, it should not print out 'four' here, the same for 'eight'. What do I misunderstand here?
number & (4 === 4)==1