Hackerrank Bitwise Operators Solution
- Published
- Updated
- Reading time
- 4 min read
- Length
- 628 words
Objective
This challenge will let you learn about bitwise operators in C.
Inside the CPU, mathematical operations like addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used which are explained below.
Bitwise AND operator &The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. It is denoted by &.Bitwise OR operator |The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. It is denoted by |.Bitwise XOR (exclusive OR) operator ^The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by .
For example, for integers 3 and 5,3 = 00000011 (In Binary)5 = 00000101 (In Binary) AND operation OR operation XOR operation 00000011 00000011 00000011& 00000101 | 00000101 ^ 00000101 ________ ________ ________ 00000001 = 1 00000111 = 7 00000110 = 6
Task
Given set , find:
- the maximum value of which is less than a given integer , where and (where ) are two integers from set .
- the maximum value of which is less than a given integer , where and (where ) are two integers from set .
- the maximum value of which is less than a given integer , where and (where ) are two integers from set .
Input Format.
The only line contains space-separated integers, and , respectively.
Constraints.
Output Format.
- The first line of output contains the maximum possible value of .
- The second line of output contains the maximum possible value of .
- The second line of output contains the maximum possible value of .
Sample Input 0
5 4
Sample Output 0.
233
Explanation 0.
All possible values of and are:
- The maximum possible value of that is also is , so we print on first line.
- The maximum possible value of that is also is , so we print on second line.
- The maximum possible value of that is also is , so we print on third line.
Solution in c#
Approach 1.
#include<stdio.h>
int main()
{
int i,j,n,k,p=0,q=0,r=0;
scanf("%d %d",&n,&k);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if((i&j) < k && p < (i&j))
{
p=i&j;
}
if((i|j) < k && q < (i|j))
{
q=i|j;
}
if((i^j) < k && r < (i^j))
{
r=i^j;
}
}
}
printf("%d\n%d\n%d",p,q,r);
}Approach 2.
#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 and, or, xor, i, j, temp;
and = 0;
or = 0;
xor = 0;
for(i=1; i<=n; i++ ){
for(j=i+1; j<=n; j++){
temp = i&j;
if(temp>and && temp<k){and = temp;}
temp = i|j;
if(temp>or && temp<k){or = temp;}
temp =i^j;
if(temp>xor && temp<k){xor = temp;}
}
}
printf("%d\n%d\n%d", and, or, xor);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}Approach 3.
#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 andmax=0,ormax=0,xormax=0;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if((i & j)>andmax && (i & j)<k)
andmax=(i & j);
if((i | j)>ormax && (i | j)<k)
ormax=(i | j);
if((i ^ j)>xormax && (i ^ j)<k)
xormax=(i ^ j);
}
}
printf("%d\n%d\n%d",andmax,ormax,xormax);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}