Bitwise Operators in C Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Bitwise Operators in C Hacker Rank Solution. This problem is a part of the HackerRank C Programming Series.

We also provide Hackerrank solutions in C++Java programming, and Python Programming languages so whatever your domain we will give you an answer in your field.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Bitwise Operators in C Hacker Rank Solution
Bitwise Operators in C – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

Bitwise Operators in C Hacker Rank Solution

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 XOR

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, s= {1,2,3,4……n} find:

  • the maximum value of a&b which is less than a given integer k , where a and b (where a < b) are two integers from set S
  • the maximum value of a|b which is less than a given integer k, where a and b (where a < b) are two integers from set S.
  • the maximum value of a XOR b which is less than a given integer k, where a and b (where a < b) are two integers from set S.

Input Format

The only line contains 2 space-separated integers n, and k, respectively.

Constraints

  •  2<=n<=10^3
  •  2<=k<=n

Output Format

The first line of output contains the maximum possible value of  a & b.
The second line of output contains the maximum possible value of  a | b.
The second line of output contains the maximum possible value of  a XOR b.


Sample Input :

5 4

Sample Output :

2
3
3

Explanation :

n = 5, k =4
S = {1,2,3,4,5}
All possible values of a and b are:

  • a = 1, b = 2 ; a&b = 0  a|b = 3  a XOR b = 3
  • a = 1, b = 3 ; a&b = 1  a|b = 3  a XOR b = 2
  • a = 1, b = 4 ; a&b = 0  a|b = 5  a XOR b = 5
  • a = 1, b = 5 ; a&b = 1  a|b = 5  a XOR b = 4
  • a = 2, b = 3 ; a&b = 2  a|b = 3  a XOR b = 1
  • a = 2, b = 4 ; a&b = 0  a|b = 6  a XOR b = 6
  • a = 2, b = 5 ; a&b = 0  a|b = 7  a XOR b = 7
  • a = 3, b = 4 ; a&b = 0  a|b = 7  a XOR b = 7
  • a = 3, b = 5 ; a&b = 1  a|b = 7  a XOR b = 6
  • a = 4, b = 5 ; a&b = 4  a|b = 5  a XOR b = 1 

The maximum possible value of a&b that is also <(k=4) is 2, so we print 2 on first line.
The maximum possible value of a|b that is also <(k=4) is 3, so we print 3 on first line.
The maximum possible value of a XOR b that is also <(k=4) is 3, so we print 3 on first line.






Bitwise Operators in C Hacker Rank Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
    int 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;
}

Disclaimer: The above Problem (Bitwise Operators in C) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

FAQ:

Is HackerRank good for C?

HackerRankHackerRank offers you to solve these programming challenges in any of various programming languages such as C, Java, Python, Ruby, etc. Apart from this, participants can solve the problems in various computer science domains like algorithms, machine learning, and artificial intelligence.

What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

Is HackerRank good for beginners?

HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

What is C programming in simple words?

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C’s features cleanly reflect the capabilities of the targeted CPUs. Wikipedia

Where can I find HackerRank solutions in C?

in this post you will get all the solutions of HackerRank C Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.

Sharing Is Caring

Leave a Comment