Magicsheet logo

Bitwise OR of Even Numbers in an Array

Easy
25%
Updated 8/1/2025

Bitwise OR of Even Numbers in an Array

What is this problem about?

In this coding problem, you are given an array of integers and asked to find the bitwise OR of only the even numbers in the array. If there are no even numbers, the result should usually be 0. This problem combines basic filtering logic with bitwise operations.

Why is this asked in interviews?

Companies like Microsoft and Meta use these simple filter-and-reduce problems to test basic coding fluency. It evaluates whether you can correctly identify even numbers (using n % 2 == 0 or (n & 1) == 0) and if you understand the identity property of the bitwise OR operation (starting with 0).

Algorithmic pattern used

This is a Simulation / Linear Scan problem. You initialize a result variable to 0, iterate through each number in the array, check if it is even, and if so, perform result |= number.

Example explanation

Array: [3, 4, 7, 10, 12]

  1. 3 is odd. Skip.
  2. 4 is even. result = 0 | 4 = 4.
  3. 7 is odd. Skip.
  4. 10 is even. result = 4 | 10 = 0100 | 1010 = 1110 (14).
  5. 12 is even. result = 14 | 12 = 1110 | 1100 = 1110 (14). Result: 14.

Common mistakes candidates make

A common error is initializing the result with the first element of the array without checking if it's even. Another mistake is using the logical OR (||) instead of the bitwise OR (|). Some candidates also forget to handle the case where the array is empty or contains only odd numbers.

Interview preparation tip

For a slightly more "pro" look, use (n & 1) == 0 to check for evenness instead of n % 2 == 0. It’s a standard bitwise trick that shows you are comfortable working at the bit level.

Similar Questions