Given two numbers, find floor of their average without using division.
Input : x = 10, y = 12 Output : 11 Input : x = 10, y = 7 Output : 8 We take floor of sum.
The idea is to use right shift operator, instead of doing (x + y)/2, we do (x + y) >> 1
Average = 15
We need floor of average of two numbers in many standard algorithms like Merge Sort, Binary Search, etc. Since we use bitwise operator instead of division, the above way of finding average is faster.
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.
- Find average of two numbers using bit operation
- Fast Exponention using Bit Manipulation
- Fast inverse square root
- Calculate 7n/8 without using division and multiplication operators
- Divide two integers without using multiplication, division and mod operator
- Cyclic Redundancy Check and Modulo-2 Division
- Compute modulus division by a power-of-2-number
- First number to leave an odd remainder after repetitive division by 2
- Sum of bit differences for numbers from 0 to N | Set 2
- Sum of numbers with exactly 2 bits set
- Bitwise AND of all the odd numbers from 1 to N
- XNOR of two numbers
- Sum of bit differences for numbers from 0 to N
- Equal Sum and XOR of three Numbers
- Maximum XOR using K numbers from 1 to n
- Find two numbers from their sum and XOR
- BCD addition of given Decimal numbers
- Find XOR of numbers from the range [L, R]
- Count of numbers having only 1 set bit in the range [0, n]
- Check whether product of 'n' numbers is even or odd
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.