/*
x = 0xff30ff30 int value;
unsigned int getBit(unsigned int x,int pos ,int n)
getBit(x,8,4)-->f //从低8位开始找4位,从右往左
getBit(x,16,8)-->30
分析:
1111 1111 0011 0000 1111 1111 0011 0000
先右移pos位
x>>pos
0000 0000 0000 0000 1111 1111 0011 0000
再用掩码
0000 0000 0000 0000 0000 0000 1111 1111
(x>>pos)&~(~0<<n)
*/
#include <stdio.h>
unsigned int getBit(unsigned int x,int pos,int n)
{
return (x>>pos)&~(~0<<n);
}
int main(int argc, char const *argv[])
{
unsigned int value = 0xff30ff30;
unsigned int result;
result = getBit(value,16,8);
printf("%x\n",result );
return 0;
}
网友评论