# C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value

Suppose we are given two integers k and n. Our task is to perform three operations; bitwise AND, bitwise OR, and bitwise XOR between all pairs of numbers up to range n. We return the maximum value of all three operations between any two pairs of numbers that is less than the given value k.

So, if the input is like n = 5, k = 5, then the output will be 4 3 4.

The greatest value of AND, OR, and XOR operations between all pairs of numbers that are less than 5 are 4, 3, and 4 respectively. We can see that the values of these operations are less than that of the given value k, which is 5.

To solve this, we will follow these steps −

• andMax := 0, orMax = 0, xorMax = 0
• value1 := 0, value2 = 0, value3 = 0
• for initialize i := 1, when i <= n, update (increase i by 1), do:
• value1 := i AND j
• value2 := i OR j
• value3 := i XOR j
• if value1 > andMax and value1 < k, then −
• andMax := value1
• if value2 > orMax and value2 < k, then −
• orMax := value2
• if value3 > xorMax and value3 < k, then −
• xorMax := value3
• print(andMax, orMax, xorMax)

## Example

Let us see the following implementation to get better understanding −

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

void solve(int n, int k) {
int andMax = 0, orMax = 0, xorMax = 0;
int value1 = 0, value2 = 0, value3 = 0;
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
value1 = i & j;
value2 = i | j;
value3 = i ^ j;
if (value1 > andMax && value1 < k)
andMax = value1;
if (value2 > orMax && value2 < k)
orMax = value2;
if (value3 > xorMax && value3 < k)
xorMax = value3;
}
}
printf("%d %d %d ", andMax, orMax, xorMax);
}
int main() {
solve(5, 5);
return 0;
}

## Input

5, 5

## Output

4 3 4