今天又开始幼稚园了

这几天任务切换把自己弄得焦头烂额,尤其是那些特权级,描述符属性什么的,真的想骂街,因此无聊的写了个打印二进制数的程序,以后就容易看描述符查属性了。

#include <stdio.h>

unsigned int do_div(unsigned int *value, unsigned int base);
char *printbin(unsigned int value);

int main(void) {
	printf("%s\n", printbin(0x92));
	printf("%s\n", printbin(0x9a));
}

unsigned int do_div(unsigned int *value, unsigned int base) {
	unsigned int temp;
	temp = *value % base;
	*value = *value / base;
	return temp;
}

char *printbin(unsigned int value) {
	char str[2] = "01";
	char tempa[128], tempb[128];
	char *temp1 = tempa, *temp2 = tempb;
	int count = 0;
	int count1 = 0;
	if(!value) {
		*temp2++ = '0';
	}
	while(value) {
		*temp1++ = str[do_div(&value, 2)];
		count++;	
		if(count % 4 == 0) {
			*temp1++ = ',';
			count1++;
		}
	}
	if (*(temp1 - 1) == ',') {
			temp1--;
			count1--;
	}
	count += count1;
	while(count) {
		*temp2++ = *--temp1;
		count--;
	}
	*temp2++ = 'b';
	*temp2 = '\0';
	temp2 = tempb;
	return temp2;
}

猜你喜欢

转载自blog.csdn.net/weixin_39410618/article/details/82975228
今日推荐