/*编程实现比特位反转,P为要反转的起始比特位,n是要反转的比特位数,
比如 0x0001 0001 ,p=4 ,n=3 反转后结果为
0x0110 0001 */
#include
#include
int bit_convert(int d, int b, int n)
{
int i;
for (i=0;i
{ if (d & (0x01<<(b+i)))
d &= ~(0x01<<(b+i));
else d |= 0x01<<(b+i);
}
return d;
}
int main(void)
{ int i;
i = bit_convert(0x51, 3, 2);
printf("Result is %x \n", i);
return 0;
}
/*-- E --*/
谢谢欣赏,欢迎指正!
阅读(3421) | 评论(0) | 转发(0) |