Algorithm


 Problem Name: Bitwise Operators

Problem Link: https://www.hackerrank.com/challenges/bitwise-operators-in-c/problem?isFullScreen=true

In this HackerRank Bitwise operators in the c programming problem solution, In this challenge, you will use logical bitwise operators. All data is stored in its binary representation. The logical operators, and C language, use 1 to represent true and 0 to represent false. The logical operators compare bits in two numbers and return true or false, 0 or 1, for each bit compared.

 

  1. 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 &.
  2. 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 |.
  3. 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 ⊕.

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#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 main() {
    int n, k,e;
  
    scanf("%d %d", &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;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5 4

Output

x
+
cmd
2 3 3
Advertisements

Demonstration


Previous
[Solved] 1D Arrays in C solution in Hackerrank - Hacerrank solution C
Next
[Solved] 1D Arrays in C solution in Hackerrank - Hacerrank solution C