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