Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

비트연산으로 홀짝 검사하기

쉬프트 연산 버전

첫 비트가 0이면 짝수 1이면 홀수 인 것에 착안.
#include <stdio.h>

int main(int argc, char **argv)
{
    char buf[80];
    while(fgets(buf, 80, stdin)!= NULL)
    {
        (atoi(buf) << 31)?printf("홀수\n"):printf("짝수\n");
    }
}

& 연산 버전

#include <stdio.h>

int main(int argc, char **argv)
{
    char buf[80];
    while(fgets(buf, 80, stdin)!= NULL)
    {
        (atoi(buf)&1)?printf("홀수\n"):printf("짝수\n");
    }
}