# Bitwise AND of all the elements of array

Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array.

Examples:

Input: arr[] = {1, 3, 5, 9, 11}
Output: 1

Input: arr[] = {3, 7, 11, 19, 11}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to traverse all the array elements and compute the bitwise AND for all the elements and print the result obtained.

Below is the implementation of above approach:

## C++

 `// C++ program to find bitwise AND ` `// of all the elements in the array ` `#include ` `using` `namespace` `std; ` `  `  `int` `find_and(``int` `arr[], ``int` `len){ ` `          `  `    ``// Initialise ans variable is arr ` `    ``int` `ans = arr; ` `  `  `    ``// 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); ` ` `  `    ``// Function Call to find AND ` `    ``cout << find_and(arr, n); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

## Java

 `// Java program to find bitwise AND ` `// of all the elements in the array ` `import` `java.util.*; ` `class` `GFG{ ` `// Function to calculate bitwise AND ` `    ``static` `int` `find_and(``int` `arr[]){ ` `         `  `        ``// Initialise ans variable is arr ` `        ``int` `ans = arr[``0``]; ` `         `  `        ``// Traverse the array compute AND ` `        ``for` `(``int` `i=``0``;i

## Python

 `# Python program to find bitwise AND ` `# of all the elements in the array ` ` `  `# Function to calculate bitwise AND ` `def` `find_and(arr): ` `     `  `    ``# Initialise ans variable is arr ` `    ``ans ``=` `arr[``0``] ` `     `  `    ``# Traverse the array compute AND ` `    ``for` `i ``in` `range``(``1``, ``len``(arr)): ` `        ``ans ``=` `ans&arr[i] ` `         `  `    ``# Return ans ` `    ``return` `ans ` `     `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``3``, ``5``, ``9``, ``11``] ` `     `  `    ``# Function Call to find AND ` `    ``print``(find_and(arr)) `

## C#

 `// C# program to find bitwise AND ` `// of all the elements in the array ` `using` `System;  ` `class` `GFG{ ` `// Function to calculate bitwise AND ` `    ``static` `int` `find_and(``int``[] arr){ ` `         `  `        ``// Initialise ans variable is arr ` `        ``int` `ans = arr; ` `         `  `        ``// Traverse the array compute AND ` `        ``for` `(``int` `i=0;i

Output:

```1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.