Joel Coding Problem count_bit

mohadang·2023년 4월 16일
0

Coding Problem

목록 보기
3/7
post-thumbnail
#include <stdio.h>
#include <assert.h>
int count_bit(unsigned char byte) {
  int ans = 0;
  while (byte != 0) {
    ans += byte & 1;
    byte >>= 1;
  }
  return ans;
}

int main() {
  assert(count_bit(7) == 3);
  assert(count_bit(1) == 1);
  assert(count_bit(0) == 0);
  assert(count_bit(8) == 1);
  assert(count_bit(16) == 1);
  assert(count_bit(255) == 8);
  assert(count_bit(256) == 0);
  return 0;
}
profile
mohadang

0개의 댓글