HackerRank C- Bitwise Operators




Given set S={1,2,3......,n}, find:

the maximum value of a&b which is less than a given integer k, where a and b (where a

the maximum value of a|b which is less than a given integer k, where a and b (where a

the maximum value of a^b which is less than a given integer k, where a and b (where a



  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. //Complete the following function.
  6. void calculate_the_maximum(int n, int k)
  7. {
  8. int max_and=0,max_or=0,max_xor=0,i=0,j=0,a=0,b=0,c=0;
  9. for(i=1;i<=n;i++)
  10. {
  11. for(j=i+1;j<=n;j++)
  12. {
  13. a=i&j;
  14. b=i|j;
  15. c=i^j;
  16. if (a<k&&a>max_and) {
  17. max_and=a;
  18. }
  19. if (b<k&&b>max_or) {
  20. max_or=b;
  21. }
  22. if (c<k&&c>max_xor) {
  23. max_xor=c;
  24. }
  25. }
  26. }
  27. printf("%d\n%d\n%d\n", max_and, max_or, max_xor);
  28. }
  29. int main() {
  30. int n, k;
  31. scanf("%d %d", &n, &k);
  32. calculate_the_maximum(n, k);
  33. return 0;
  34. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube