【C语言】取一个整数a从右端开始的4~7位

程序分析

(1)先使a右移4位
(2)设置一个低4位全为1,其余全为0的数。可用~ (~0<<4)。
(3)将上面二者进行&运算

源代码

#include
#include

int main()
{
	unsigned a, b, c, d;
	scanf("%o", &a);
	b = a >> 4;
	c = ~(~0 << 4);
	d = b & c;
	printf("%o\n%o\n", a, d);
	system("pause");
	return 0;
}

你可能感兴趣的:(C语言)