Bitwise Operators in C hackerRank step by step solution

Bitwise Operators in C hackerRank step by step solution:

   Welcome back, guys!
    In today's post, we will look at Bitwise Operators in C hackerRank practice problem.
    It requires basic knowledge of  Loops and Bitwise operators.

The following table lists the Bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −
OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) = 12, i.e., 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) = 61, i.e., 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) = 49, i.e., 0011 0001
~Binary One's Complement Operator is unary and has the effect of 'flipping' bits.(~A ) = ~(60), i.e,. 1100 0011
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 = 240 i.e., 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 = 15 i.e., 0000 1111

Problem Statement for Bitwise Operators :


  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
2
3
3
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 for Bitwise operators :
 

Here is a breakdown of the problem:

1)We are given numbers,n(the size of our set) and k (a constraint on the numbers we choose for our output).

2)We must have a set of integers from 1 to n that we'll refer to as S.

3)We must have 2 integers, a and b, that are elements of S. These are constrained by the rule that a<b. We implement this with nested loops.

4)We must find the bitwise AND, bitwise OR, and bitwise XOR for every possible a and b. We'll refer to these as x, y, and z respectively.

5)We must print the maximum x<k, y<k, and z<k denoted by max_and,max_or, and max_xor respectively in the output.



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.


void calculate_the_maximum(int n, int k) {
    int i,j,max_and=0,max_or=0,max_xor=0;
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            int x=i&j,y=i|j,z=i^j;
            if(x<k & x>max_and)
                 max_and=x;
            if(y<k & y>max_or)
                 max_or = y;
            if(z<k & z>max_xor)
                 max_xor = z;       

        }
    }
    printf("%d\n%d\n%d",max_and,max_or,max_xor);
}

int main() {
    int n, k;
  
    scanf("%d %d", &n, &k);
    calculate_the_maximum(n, k);
    return 0;
}


Hope it adds knowledge to you.
Analysis the code properly.
Feel Free to post your doubts in the comments down below.
See you next time.













Post a Comment

2 Comments