Bitwise AND of all the elements of array - GeeksforGeeks

28 Apr, 2022
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  
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++14
C
Java
Python3
C#
Javascript
// 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;} // Driver functionint 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);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// C program to find bitwise AND of all the elements in the array#include <stdio.h> 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;} // Driver functionint main(){    int arr[] = { 1, 3, 5, 9, 11 };    int n = sizeof(arr) / sizeof(arr[0]);    // Function Call to find AND    printf("%d",find_and(arr, n));    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// Java program to find bitwise AND// of all the elements in the arrayimport java.util.*;class GFG {    // Function to calculate bitwise AND    static int find_and(int arr[])    {        // Initialise ans variable is arr[0]        int ans = arr[0];        // Traverse the array compute AND        for (int i = 0; i < arr.length; i++)            ans = (ans & arr[i]);        return ans;    }    // Driver Code    public static void main(String args[])    {        int arr[] = { 1, 3, 5, 9, 11 };        // Function Call to find AND        System.out.println(find_and(arr));    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python program to find bitwise AND# of all the elements in the array # Function to calculate bitwise ANDdef find_and(arr):         # Initialise ans variable is arr[0]    ans = arr[0]         # Traverse the array compute AND    for i in range(1, len(arr)):        ans = ans&arr[i]             # Return ans    return ans     # Driver Codeif __name__ == '__main__':    arr = [1, 3, 5, 9, 11]         # Function Call to find AND    print(find_and(arr))
// C# program to find bitwise AND// of all the elements in the arrayusing System;class GFG{// Function to calculate bitwise AND    static int find_and(int[] arr){                 // Initialise ans variable is arr[0]        int ans = arr[0];                 // Traverse the array compute AND        for (int i=0;i<arr.Length;i++){            ans = (ans&arr[i]);        }        // Return ans        return ans;    }    // Driver Code    public static void Main()    {        int[] arr = {1, 3, 5, 9, 11};                 // Function Call to find AND        Console.Write(find_and(arr));    }} // This code is contributed by AbhiThakur
<script> // Javascript program to find bitwise AND// of all the elements in the array // Function to calculate bitwise ANDfunction find_and(arr){         // Initialise ans variable is arr[0]    let ans = arr[0];           // Traverse the array compute AND    for(let i = 0; i < arr.length; i++)    {        ans = (ans&arr[i]);    }         // Return ans    return ans;} // Driver Codelet arr = [ 1, 3, 5, 9, 11 ]; // Function Call to find ANDdocument.write(find_and(arr));     // This code is contributed by unknown2108 </script>
1
 
abhaysingh290895
sapnasingh4991
unknown2108
adityakumar129
Bitwise-AND
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery