#include <stdio.h>

unsigned int getbits(unsigned x, int p, int n)
{
   return (x>>(p-n)) & ~(~0<<n); 
    /* 312는 2진수 표현하면100111000을 오른쪽으로 4칸 시프트 연산을 해서 000010011
     ~0은 0의 보수이므로 00000000을 11111111이고 이를 4칸 왼쪽 시프트하면 11110000이다.
     그리고 괄호 밖의 ~으로 다시 보수를 취하면 00001111을 만들고 이를 위의 내용과 &연산을 하면 00000011이 된다. */
}
void main() {
   unsigned int x=312, res;
    res = getbits(x, 8, 4);
    printf("%d\n", res);
    }

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: