Answers for "Bitwise AND of all the elements of array"

C++
0

Bitwise AND of all the elements of array

// C++ program to find bitwise AND of all the elements in the array
#include <bits/stdc++.h>
using namespace std;
  
int find_and(int arr[], int len){
          
    // Initialise ans variable is arr[0]
    int ans = arr[0];
  
    // Traverse the array compute AND
    for (int i = 0; i < len; i++){
        ans = (ans&arr[i]);
    }
    // Return ans
    return ans;
}
 
// Driver function
int main()
{
    int arr[] = {1, 3, 5, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call to find AND
    cout << find_and(arr, n);
}
Posted by: Guest on October-10-2021

Code answers related to "Bitwise AND of all the elements of array"

Browse Popular Code Answers by Language