I am trying to assess if the numbers inside an array are even or odd using the bitwise operator &.
This is my code:
arr = [1, 2, 4, 6, 9, 11, 18, 361, 5622, 5623]
arr.forEach(function (el) {
if (parseInt(el.toString(2)) & 1 === 0) {
console.log(`${el} in binary is ${el.toString(2)} and it is an EVEN number`);
} else {
console.log(`${el} in binary is ${el.toString(2)} and it is an ODD number`);
}
});
Taking each element individually each one returns 0 or 1 which would assess if a number is even or odd, but in this case they all return ODD numbers. This is what I get as result:
1 in binary is 1 and it is an ODD number
2 in binary is 10 and it is an ODD number
4 in binary is 100 and it is an ODD number
and so on...
Where did I do wrong?