bitwise AND OR NOT operations between numbers c program
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k) {
//Write your code here.
int a1=0,o1=0,xo1=0,tmp1,tmp2,tmp3;
int s[n+1],i,j;
for(i=0;i<n;i++){
s[i]=i+1;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(o1<s[i]|s[j]){
tmp1=s[i]|s[j];
if(tmp1<k){
o1=s[i]|s[j];
}
}
tmp2=s[i]&s[j];
if(a1<tmp2){
if(k>tmp2) {a1=s[i]&s[j];
// for testing printf("\n %d&%d=%d & k=%d",s[i],s[j],s[i]&s[j],k);
}
}
tmp3=s[i]^s[j];
if(xo1<tmp3){
if(k>tmp3) {
xo1=s[i]^s[j];
}
}
}
}
printf("%d\n",a1);
printf("%d\n",o1);
printf("%d\n",xo1);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
Comments
Post a Comment