카타 7급 Power of two

C[ | ]

#include <stdbool.h>
bool power_of_two(const int x) {
  int power = 1;
  while (power < x) power <<= 1;
  return (power == x);
}
#include <stdbool.h>
bool power_of_two(const int x) {
    if(x > 0 && (x & (x-1)) == 0) return true;
    return false;
}
#include <stdbool.h>
bool power_of_two(const int x) {
   return x && !(x - (x & -x));
}
#include <stdbool.h>
bool power_of_two(const int x) {
  int n = x;
  while( n > 1 ) {
    if( n%2 != 0 ) return false;
    n /= 2;
  }
  return n == 1;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}