1. 程式人生 > >今天又開始幼稚園了

今天又開始幼稚園了

這幾天任務切換把自己弄得焦頭爛額,尤其是那些特權級,描述符屬性什麼的,真的想罵街,因此無聊的寫了個列印二進位制數的程式,以後就容易看描述符查屬性了。

#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;
}