Compute maximum of two integers in C / C ++ using Bitwise Operators | C ++ program for above approach ; Function to find the largest number ; Perform the subtraction ; Right shift and Bitwise AND ; Find the maximum number ; Return the maximum value ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int findMax ( int a , int b ) { int z , i , max ; z = a - b ; i = ( z >> 31 ) & 1 ; max = a - ( i * z ) ; return max ; } int main ( ) { int A = 40 , B = 54 ; cout << findMax ( A , B ) ; return 0 ; }