C语言位运算实现原码一位乘法 3寄存器和2寄存器版本 计组

简介

计组中讲到用逻辑电路实现原码一位乘法,要求用位运算模拟(数组它不好吗。。。),CSAPP中介绍了原码一位乘法的改进本版本,把乘数和结果都放到一个寄存器,结果存放到寄存器的高32位,乘数放到低32位。每次运算把乘数最低为右移去掉。

输出部分可以单独设计函数。。作业截止期过后放上来

3寄存器

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

unsigned long long int multiply3(unsigned long long int x, unsigned long long int y)
{
    unsigned long long int result,i;
    char sx[32],sy[32],s[32]; //不必要,仅用于查看过程中的二进制运算
    result = 0;
    
    
    int tx = (int)x;
    int ty = (int)y;
    int ts;
    itoa(tx, sx, 2);	//转二进制写入数组
    itoa(ty, sy, 2);
    unsigned len = strlen(sy);  //减少不必要的循环次数

    printf("\t\t\t\tX \t\t\t\tY \t\t\t\tResult\n");
    printf("%32s %32s\n", sx, sy);

    for (i = 0; i < len; i++) 
    {  //关键的五行代码
        if ((y & 1) == 1)
        {
            result += x;
        }
        x = x << 1;
        y = y >> 1;

        tx = (int)x;
        ty = (int)y;
        ts = (int)result;
        itoa(tx, sx, 2);
        itoa(ty, sy, 2);
        itoa(ts, s, 2);
        printf("%32s %32s %32s\n", sx, sy,s);
    }
    return result; 
}

int main()
{
	unsigned long long int x = 9;
	unsigned long long int y_p = 11;
    unsigned long long int re;

    re = multiply3(x, y_p);
    int t = (int)re;
    itoa(t, sx, 2);
    printf("%d-%s\n", t, sx);

	system("pause");
	return 0;
}

2寄存器

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

unsigned long long int multiply2(unsigned long long int x, unsigned long long int y)
{
    unsigned  int  i;
    char sx[32], sy[32];

    int tx = (int)x;
    int ty = (int)y;
    int ts;
    itoa(tx, sx, 2);
    itoa(ty, sy, 2);
    unsigned int len = strlen(sy);  
    printf("\t\t\t\t\bX \t\t\t\t\b\bY_P\n");
    printf("%32s %32s\n", sx, sy);

    for (i = 0; i < len; i++)
    {
        if ((y & 1) == 1)
        {
            y = y + (unsigned)( x <<(len - i) );
        }
        x = x << 1;
        y = y >> 1;

        tx = (int)x;
        ty = (int)y;

        itoa(tx, sx, 2);
        itoa(ty, sy, 2);

        printf("%32s %32s\n", sx, sy);
    }
    return y;
}

int main()
{
	unsigned long long int x = 9;
	unsigned long long int y_p = 11;
    unsigned long long int re;

	char sx[32];
    re = multiply2(x, y_p);
    int t = (int)re;
    itoa(t, sx, 2);
    printf("%d-%s\n", t, sx);

	system("pause");
	return 0;
}

有朋友说上面x左移了不是原码一位乘法了!!!那就修改一下嘛

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

unsigned long long int multiply3(unsigned long long int x, unsigned long long int y)
{
    unsigned long long int result,i;
    char sx[32],sy[32],s[32]; //不必要,仅用于查看过程中的二进制运算
    result = 0;
    
    
    int tx = (int)x;
    int ty = (int)y;
    int ts;
    itoa(tx, sx, 2);	//转二进制写入数组
    itoa(ty, sy, 2);
    unsigned len = strlen(sy);  //减少不必要的循环次数

    printf("\t\t\t\tX \t\t\t\tY \t\t\t\tResult\n");
    printf("%32s %32s\n", sx, sy);

    for (i = 0; i < len; i++) 
    {  //关键的五行代码
        if ((y & 1) == 1)
        {
            result += x;
        }
        x = x << 1;
        y = y >> 1;

        tx = (int)x;
        ty = (int)y;
        ts = (int)result;
        itoa(tx, sx, 2);
        itoa(ty, sy, 2);
        itoa(ts, s, 2);
        printf("%32s %32s %32s\n", sx, sy,s);
    }
    return result; 
}

int main()
{
	unsigned long long int x = 9;
	unsigned long long int y_p = 11;
    unsigned long long int re;

    re = multiply3(x, y_p);
    int t = (int)re;
    itoa(t, sx, 2);
    printf("%d-%s\n", t, sx);

	system("pause");
	return 0;
}

2寄存器

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

unsigned long long int multiply2(unsigned long long int x, unsigned long long int y)
{
    unsigned  int  i;
    char sx[32], sy[32];

    int tx = (int)x;
    int ty = (int)y;
    int ts;
    itoa(tx, sx, 2);
    itoa(ty, sy, 2);
    unsigned int len = strlen(sy);  
    printf("\t\t\t\t\bX \t\t\t\t\b\bY_P\n");
    printf("%32s %32s\n", sx, sy);

    for (i = 0; i < len; i++)
    {
        if ((y & 1) == 1)
        {
            y = y + (unsigned)( x <<len  );//这里补上左移
        }
        //x = x << 1;去掉x左移
        y = y >> 1;

        tx = (int)x;
        ty = (int)y;

        itoa(tx, sx, 2);
        itoa(ty, sy, 2);

        printf("%32s %32s\n", sx, sy);
    }
    return y;
}

int main()
{
	unsigned long long int x = 9;
	unsigned long long int y_p = 11;
    unsigned long long int re;

	char sx[32];
    re = multiply2(x, y_p);
    int t = (int)re;
    itoa(t, sx, 2);
    printf("%d-%s\n", t, sx);

	system("pause");
	return 0;
}

参考

  • https://blog.csdn.net/weixin_42109012/article/details/103420398
  • CSAPP
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值