Bitwise Operators in C - Hacker Rank Solution
Bitwise Operators in C - Hacker Rank Solution |
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 =4S = {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 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.
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #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 whole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
first one is not working , timeout error is shown
The solution is Correct and it's work.
correct
It's not working.....