Find average of two numbers using bit operation Last Updated : 30 May, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 3Input: 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; } 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) Comment More infoAdvertise with us Next Article Find average of two numbers using bit operation R Rajnis09 Follow Improve Article Tags : Bit Magic Algorithms Mathematical DSA Numbers +1 More Practice Tags : AlgorithmsBit MagicMathematicalNumbers Similar Reads Find XOR of two number without using XOR operator Given two integers, the task is to find XOR of them without using the XOR operator.Examples : Input: x = 1, y = 2Output: 3Input: x = 3, y = 5Output: 6Approach - Checking each bit - O(log n) time and O(1) spaceA Simple Solution is to traverse all bits one by one. For every pair of bits, check if both 8 min read Find the Number Using Bitwise Questions I Given a task to find a number n. There is a pre-defined API int commonSetBits(int val) that returns the number of bits where both n and val have a value of 1 in the corresponding position of their binary representation. In other words, it returns the number of set bits in the bitwise AND (&) ope 4 min read Check if two numbers are bit rotations of each other or not Given two positive integers x and y (0 < x, y < 2^32), check if one integer is obtained by rotating bits of the other. Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. Examples: Input : 6 min read Add two numbers without using arithmetic operators Given two integers a and b, the task is to find the sum of a and b without using + or - operators. Examples: Input: a = 10, b = 30Output: 40Input: a = -1, b = 2Output: 1Approach:The approach is to add two numbers using bitwise operations. Let's first go through some observations: a & b will have 5 min read Count of carry operations on adding two Binary numbers Given two decimal numbers num1 and num2, the task is to count the number of times carry operation is required while adding the two given numbers in binary form. Examples: Input: num1 = 15, num2 = 10Output: 3Explanation:Give numbers are added as:15 -> 1 1 1 110 -> 1 0 1 0carry -> 1 1 1 - --- 6 min read Like