LearnFrenzy
Sign in Take a free test

Bitwise Operators · Predict the Output · Question 9

Power-of-two check n & (n - 1) – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%d %d\n", (16 & 15) == 0, (18 & 17) == 0);
    return 0;
}
  • A 1 0 Correct
  • B 0 1
  • C 1 1
  • D 0 0
Correct Answer: Option A

16 = 10000 and 15 = 01111 share no bits, so the AND is 0: a power of two. 18 = 10010 and 17 = 10001 share the top bit (16), so 18 is not a power of two.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links