coder18s,blogspot.com

Powered by Blogger.

9.Bitwise Operators in C Hackerrank Solution

Task

Print the maximum values for the andor and xor comparisons, each on a separate line.

Example

The results of the comparisons are below:

a  b  and or  xor
1  2   0    3   3
1  3   1    3   2
2  3   2    3   1

Sample Input 0

5    4

Sample Output 0

2
3
3
                Program Code

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

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    int mxAnd = 0, mxOr = 0, mxXor = 0;
    
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(mxAnd < (i & j) && (i & j) < k)
                mxAnd = i & j;
            if(mxOr < (i | j) && (i | j) < k)
                mxOr = i | j;
            if(mxXor < (i ^ j) && (i ^ j) < k)
                mxXor = i ^ j;
        }
    }
    printf("%d\n", mxAnd);
    printf("%d\n", mxOr);
    printf("%d\n", mxXor);
 
    return 0;
}