#include
#include
main()
{
printf("**********************************\n");
printf(" 本程序用于实现字节乘法\n");
printf("**********************************\n\n");
printf("请输入2个十六进制数(如:aa bb):\n");
int a,b,i;
scanf("%x %x",&a,&b);
//将十六进制的个位与十位分开
int agewei,ashiwei,bgewei,bshiwei;
agewei=a%16;
ashiwei=a/16;
bgewei=b%16;
bshiwei=b/16;
//将十六进制转换为二进制
int shuzua[8][8]={0},shuzub[8]={0};
int yushu,shang;
for(i=3;i>=0;i--){
yushu=ashiwei%2;
shuzua[0][i]=yushu;
ashiwei=ashiwei/2;
}
for(i=7;i>=4;i--){
yushu=agewei%2;
shuzua[0][i]=yushu;
agewei=agewei/2;
}
for(i=3;i>=0;i--){
yushu=bshiwei%2;
shuzub[i]=yushu;
bshiwei=bshiwei/2;
}
for(i=7;i>=4;i--){
yushu=bgewei%2;
shuzub[i]=yushu;
bgewei=bgewei/2;
}
//第一个数为s(x),第二个数为t(x)。
printf("2个十六进制数的二进制为:\n");
printf(" s(x) = ");
for(i=0;i<8;i++){
printf("%d",shuzua[0][i]);
}
printf(" ");
printf("t(x) = ");
for(i=0;i<8;i++){
printf("%d",shuzub[i]);
}
printf("\n那么:\n");
int guding[8]={0,0,0,1,1,0,1,1};
int temp=0,j;
for(j=1;j<8;j++){
i=0;
if(shuzua[j-1][i]==0){
for(i=0;i<7;i++){
shuzua[j][i]=shuzua[j-1][i+1];
}
shuzua[j][7]=temp;
}else if(shuzua[j-1][i]==1){
for(i=0;i<7;i++){
shuzua[j][i]=shuzua[j-1][i+1];
}
shuzua[j][7]=temp;
for(i=0;i<8;i++){
shuzua[j][i]=shuzua[j][i]^guding[i];
}
}
printf("x的%d次方*s(x) = ",j);
for(i=0;i<8;i++){
printf("%d",shuzua[j][i]);
}
printf("\n");
}
int shuzuc[8][8]={0};
for(i=0;i<8;i++){
if(shuzub[i]==1){
for(j=0;j<8;j++){
shuzuc[i][j]=shuzua[7-i][j];
}
}
}
int digit=0,sum=0,yi[8];
for(i=0;i<8;i++){
for(j=0;j<8;j++){
sum=sum+shuzuc[i][j];
}
if(sum>=1){
yi[digit]=i;
digit++;
}
sum=0;
}
int jieguo[8]={0};
for(i=0;i
if(i==0){
for(j=0;j<8;j++){
jieguo[j]=shuzuc[yi[i]][j];
}
}else{
for(j=0;j<8;j++){
jieguo[j]=jieguo[j]^shuzuc[yi[i]][j];
}
}
}
printf("计算结果为:\n");
for(i=0;i<8;i++){
printf("%d",jieguo[i]);
}
printf("\n转换为16进制为:\n");
int qianhe,houhe;
qianhe=jieguo[3]*pow(2,0)+jieguo[2]*pow(2,1)+jieguo[1]*pow(2,2)+jieguo[0]*pow(2,3);
houhe=jieguo[7]*pow(2,0)+jieguo[6]*pow(2,1)+jieguo[5]*pow(2,2)+jieguo[4]*pow(2,3);
printf("%x %x",qianhe,houhe);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)