[ create a new paste ] login | about

Link: http://codepad.org/D83lLvNG    [ raw code | output | fork ]

C, pasted on Dec 9:
#include<stdio.h>

//BCD : Binary-Coded Decimal
void BCD_Representation(unsigned int data,unsigned int count[])
{
	unsigned int i;
	unsigned int bin[32];

	for(i=0;i<32;i++){
		bin[i]=data%2;
		data/=2;
	}

	count[0]=count[1]=0;
	for(i=0;i<32;i++){
		if(bin[31-i]==0)count[0]++;			//0および1の個数を数える
		if(bin[31-i]==1)count[1]++;
		//count[bin[31-i]]++;				//←これでも可能

		printf("%d",bin[31-i]);
		if(i%4==3)							//4桁ずつ区切る
			printf(" ");
	}
}

int main(void)
{
	unsigned int data1,data2,count[2];

	printf("符号なし32bit整数を入力してください.\n");
	scanf("%d",&data1);

	printf("%d は2進表示で ",data1);
	BCD_Representation(data1,count);
	printf("です.\n");
	printf("0のビット数は%d,1のビット数は%dです.\n",count[0],count[1]);
	printf("\n");


	printf("符号なし32bit整数2つを入力してください.\n");
	scanf("%d%d",&data1,&data2);

	printf("%d は2進表示で ",data1);
	BCD_Representation(data1,count);
	printf(",\n");
	printf("%d は2進表示で ",data2);
	BCD_Representation(data2,count);
	printf("です.\n");

	printf("~%d は2進表示で ",data1);
	BCD_Representation(~data1,count);
	printf(",\n");
	printf("~%d は2進表示で ",data2);
	BCD_Representation(~data2,count);
	printf("です.\n");

	printf("%d << 1 は2進表示で ",data1);
	BCD_Representation(data1<<1,count);
	printf(",\n");
	printf("%d >> 1 は2進表示で ",data2);
	BCD_Representation(data2>>1,count);
	printf("です.\n");

	printf("%d & %d は2進表示で ",data1,data2);
	BCD_Representation(data1&data2,count);
	printf("です.\n");
	printf("%d | %d は2進表示で ",data1,data2);
	BCD_Representation(data1|data2,count);
	printf("です.\n");
	printf("%d ^ %d は2進表示で ",data1,data2);
	BCD_Representation(data1^data2,count);
	printf("です.\n");

	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
符号なし32bit整数を入力してください.
134513822 は2進表示で 0000 1000 0000 0100 1000 0100 1001 1110 です.
0のビット数は23,1のビット数は9です.

符号なし32bit整数2つを入力してください.
134513822 は2進表示で 0000 1000 0000 0100 1000 0100 1001 1110 ,
1073830340 は2進表示で 0100 0000 0000 0001 0101 1001 1100 0100 です.
~134513822 は2進表示で 1111 0111 1111 1011 0111 1011 0110 0001 ,
~1073830340 は2進表示で 1011 1111 1111 1110 1010 0110 0011 1011 です.
134513822 << 1 は2進表示で 0001 0000 0000 1001 0000 1001 0011 1100 ,
1073830340 >> 1 は2進表示で 0010 0000 0000 0000 1010 1100 1110 0010 です.
134513822 & 1073830340 は2進表示で 0000 0000 0000 0000 0000 0000 1000 0100 です.
134513822 | 1073830340 は2進表示で 0100 1000 0000 0101 1101 1101 1101 1110 です.
134513822 ^ 1073830340 は2進表示で 0100 1000 0000 0101 1101 1101 0101 1010 です.


Create a new paste based on this one


Comments: