-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitwiseOperatorsHR.c
82 lines (58 loc) · 2.43 KB
/
BitwiseOperatorsHR.c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
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.
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
*/
// SOLUTION
#include <stdio.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k) {
//Write your code here.
int maxAnd = 0 , maxOr = 0 , maxXor = 0 ,i,j;
for (i = 1 ; i<=n ;i++)
{
for(j=i+1;j<=n;j++)
{
if((maxAnd <= (i&j)) && ((i&j) < k)){maxAnd = i&j;}
if((maxOr <= (i|j)) && ((i|j) < k)){maxOr = i|j;}
if((maxXor <= (i^j)) && ((i^j) < k)){maxXor = i^j;}
}
}
printf("%d\n%d\n%d",maxAnd,maxOr,maxXor);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}