Bitwise Operators - Hacker Rank Solution
- We are given numbers, (the size of our set) and (a constraint on the numbers we choose for our output).
- We must have a set of integers from to that we'll refer to as .
- We must have integers, and , that are elements of . These are constrained by the rule that . We implement this with nested loops.
- We must find the bitwise AND, bitwise OR, and bitwise XOR for every possible and . We'll refer to these as , , and respectively.
- We must print the maximum , , and denoted by , , and respectively in the output.
void calculate_the_maximum(int n, int k) { int maximum_and = 0, maximum_or = 0, maximum_xor = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int x = i&j, y = i|j, z = i^j;
if (x < k && x > maximum_and) {
maximum_and = x;
}
if (y < k && y > maximum_or) {
maximum_or = y;
}
if (z < k && z > maximum_xor) {
maximum_xor = z;
}
}
}
printf("%d\n%d\n%d\n", maximum_and, maximum_or, maximum_xor);
}
Good
ReplyDeletei tried this one but i got wrong answers only :(
ReplyDeleteBitwise Operators in C – Hacker Rank Solution
ReplyDeletehttps://www.codeworld19.com/bitwise-operators-in-c-hacker-rank-solution/