Find average of two numbers using bit operation

Given two integers x and y, the task is to find the average of these numbers i.e. (x + y) / 2 using bit operations. Note that this method will give result as floor value of the calculated average.
Examples: 
 

Input: x = 2, y = 4 
Output:
(2 + 4) / 2 = 3
Input: x = 10, y = 9 
Output:
 

 

Approach: Average of two numbers x and y can be calculated using bit operations as: 
 

(x & y) + ((x ^ y) >> 1) 
 

where & is bitwise AND, ^ is bitwise XOR and >> 1 is right shift by 1 bit.
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the average
// of x and y using bit operations
int getAverage(int x, int y)
{
    // Calculate the average
    // Floor value of (x + y) / 2
    int avg = (x & y) + ((x ^ y) >> 1);
 
    return avg;
}
 
// Driver code
int main()
{
    int x = 10, y = 9;
 
    cout << getAverage(x, y);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the average
    // of x and y using bit operations
    static int getAverage(int x, int y)
    {
 
        // Calculate the average
        // Floor value of (x + y) / 2
        int avg = (x & y) + ((x ^ y) >> 1);
 
        return avg;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int x = 10, y = 9;
 
        System.out.print(getAverage(x, y));
    }
}


Python




# Python 3 implementation of the approach
 
# Function to return the average
# of x and y using bit operations
def getAverage(x, y):
 
    # Calculate the average
    # Floor value of (x + y) / 2
    avg = (x & y) + ((x ^ y) >> 1);
     
    return avg
 
# Driver code
x = 10
y = 9
print(getAverage(x, y))


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the average
    // of x and y using bit operations
    static int getAverage(int x, int y)
    {
 
        // Calculate the average
        // Floor value of (x + y) / 2
        int avg = (x & y) + ((x ^ y) >> 1);
 
        return avg;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 10, y = 9;
 
        Console.WriteLine(getAverage(x, y));
    }
}
 
// This code is contributed by AnkitRai01


PHP




<?php
// PHP implementation of the approach
 
// Function to return the average
// of x and y using bit operations
function getAverage($x, $y)
{
    // Calculate the average
    // Floor value of (x + y) / 2
    $avg = ($x & $y) + (($x ^ $y) >> 1);
 
    return $avg;
}
 
// Driver code
    $x = 10;
    $y = 9;
 
    echo getAverage($x, $y);
 
// This code is contributed by ajit.
?>


Javascript




<script>
// javascript implementation of the approach   
 
    // Function to return the average
    // of x and y using bit operations
    function getAverage(x , y)
    {
 
        // Calculate the average
        // Floor value of (x + y) / 2
        var avg = (x & y) + ((x ^ y) >> 1);
 
        return avg;
    }
 
    // Driver code
    var x = 10, y = 9;
    document.write(getAverage(x, y));
 
// This code is contributed by Rajput-Ji
</script>


Output: 

9

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button