Given two integers A and B, the task is to find the maximum of two numbers using Bitwise Operators.
Examples:
Input: A = 40, B = 54
Output: 54Input: A = -1, B = -10
Output: -1
Approach: The idea is to use the Bitwise Operator as well as the Right Shift Operator to find the greatest number between two distinct numbers without using any conditional statements( if … ) and Ternary Operator(?: ). Below are the steps:
- Find the maximum value on the basis of the below expression:
z = A – B
i = (z >> 31) & 1
max = a – (i*z)
- Subtract two numbers and store it in another variable z.
- To get the sign of the number obtained after subtraction, apply Right Shift to the variable z and store it in another variable i and then perform Bitwise AND operation on the variable i with 1 to get values in 1 or 0.
- Perform the following expression to get the largest value among the two given numbers as max = (a – (i * z)).
Illustration:
A = 40, B = 54
z = (A – B) = 40 – 54 = -14
i = -1 & 1 = 1
max = a – (i * z) = (40 – (1 * -14)) = 54
Below is the implementation of the above approach:
C++
// C++ program for above approach#include <iostream>using namespace std;Â
// Function to find the largest numberint findMax(int a, int b){Â Â Â Â int z, i, max;Â
    // Perform the subtraction    z = a - b;Â
    // Right shift and Bitwise AND    i = (z >> 31) & 1;Â
    // Find the maximum number    max = a - (i * z);Â
    // Return the maximum value    return max;}Â
// Driver Codeint main(){Â Â Â Â int A = 40, B = 54;Â
    // Function Call    cout << findMax(A, B);Â
    return 0;} |
C
// C program for the above approach#include <stdio.h>Â
// Function to find the largest numberint findMax(int a, int b){Â Â Â Â int z, i, max;Â
    // Perform the subtraction    z = a - b;Â
    // Right shift and Bitwise AND    i = (z >> 31) & 1;Â
    // Find the maximum number    max = a - (i * z);Â
    // Return the maximum value    return max;}Â
// Driver Codeint main(){Â Â Â Â int A = 40, B = 54;Â
    // Function Call    printf("%d", findMax(A, B));Â
    return 0;} |
Java
// Java program for above approachimport java.io.*;Â
class GFG {Â
  // Function to find the largest number  public static int findMax(int a, int b)  {    int z, i, max;Â
    // Perform the subtraction    z = a - b;Â
    // Right shift and Bitwise AND    i = (z >> 31) & 1;Â
    // Find the maximum number    max = a - (i * z);Â
    // Return the maximum value    return max;  }Â
  // Driver Code  public static void main (String[] args)  {    int A = 40, B = 54;Â
    // Function Call    System.out.println(findMax(A, B));  }Â
}Â
// This code is contributed by Shubham Singh |
Python3
# Python program for the above approachÂ
# Function to find the largest numberdef findmaxx(a, b):         # Perform the subtraction    z = a - b         # Right shift and Bitwise AND    i = (z >> 31) & 1         # Find the maxximum number    maxx = a - (i * z)         # Return the maxximum value    return maxxÂ
# Driver CodeA = 40B = 54Â
# Function Callprint(findmaxx(A, B))Â
# This code is contributed by Shubham Singh |
Javascript
<script>// Javascript program for above approachÂ
// Function to find the largest numberfunction findMax(a, b){Â Â Â Â var z, i, max;Â
    // Perform the subtraction    z = a - b;Â
    // Right shift and Bitwise AND    i = (z >> 31) & 1;Â
    // Find the maximum number    max = a - (i * z);Â
    // Return the maximum value    return max;}Â
// Driver Codevar A = 40, B = 54;Â
// Function Calldocument.write(findMax(A, B));Â
// This code is ocntributed by shubham singh</script> |
54
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
