Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Created November 22, 2018 19:46
Show Gist options
  • Save dbc2201/b59b19ad8afa0f9700b7c8e61d07dc18 to your computer and use it in GitHub Desktop.
Save dbc2201/b59b19ad8afa0f9700b7c8e61d07dc18 to your computer and use it in GitHub Desktop.
Source Code for the Bitwise operators problem in HackerRank located here https://www.hackerrank.com/challenges/bitwise-operators-in-c/problem
#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 i = 0, j = 0;
int and, and_max = 0;
int or, or_max = 0;
int xor, xor_max = 0;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i < j )
{
and = i & j;
if ( and > and_max && and < k )
{
and_max = and;
}
or = i | j;
if ( or > or_max && or < k )
{
or_max = or;
}
xor = i ^ j;
if ( xor > xor_max && xor < k )
{
xor_max = xor;
}
}
}
}
printf("%d\n%d\n%d", and_max, or_max, xor_max);
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
@nik-1207
Copy link

Thank you sir I got the point

@kumail-tech
Copy link

awesome code sirrr

@Siddharth18-dev
Copy link

#include<stdio.h>
main()
{
int n,k,a,b,man=0,mor=0,mxor=0;
    scanf("%d%d",&n,&k);

for(a=1;a<n;a++)
  {
    for(b=a+1;b<=n;b++)
      {
          if(a&b<k)
           {
               if(man<a&b)
                  man=a&b;
           }
          if(a|b<k)
           {
               if(mor<a|b)
                  mor=a|b;
           } 
          if(a^b<k)
           {
               if(mxor<a^b)
                  mxor=a^b;
           }
      }
  }
  printf("%d \n%d \n%d",man,mor,mxor); 
}
```Whats wrong with this, can anyone help me!!

@shubhamkumar37924
Copy link

#include<stdio.h>
main()
{
int n,k,a,b,man=0,mor=0,mxor=0;
    scanf("%d%d",&n,&k);

for(a=1;a<n;a++)
  {
    for(b=a+1;b<=n;b++)
      {
          if(a&b<k)
           {
               if(man<a&b)
                  man=a&b;
           }
          if(a|b<k)
           {
               if(mor<a|b)
                  mor=a|b;
           } 
          if(a^b<k)
           {
               if(mxor<a^b)
                  mxor=a^b;
           }
      }
  }
  printf("%d \n%d \n%d",man,mor,mxor); 
}
```Whats wrong with this, can anyone help me!!

in for loop there should be 5 instead of value n. correct it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment