# 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:3

(2 + 4) / 2 = 3

Input:x = 10, y = 9

Output:9

**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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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)) ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

9

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the average of first N natural numbers
- Average of first n even natural numbers
- Average of first n odd naturals numbers
- Average of a stream of numbers
- Average of Cubes of first N natural numbers
- Average of odd numbers till a given odd number
- Average of even numbers till a given even number
- Fast average of two numbers without division
- Compute average of two numbers without overflow
- Average of Squares of Natural Numbers
- Count the numbers which can convert N to 1 using given operation
- Count of numbers which can be made power of 2 by given operation
- Product of Complex Numbers using three Multiplication Operation
- Find the subarray with least average
- Find the Batting Average of a batsman
- Program to find simple moving average
- Find the deleted value from the array when average of original elements is given
- Find the ratio of number of elements in two Arrays from their individual and combined average
- Find letter's position in Alphabet using Bit operation
- Find the index which is the last to be reduced to zero after performing a given operation

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.