棋子

导航

20150328晚,不使用乘号运算符实现两个整数乘法

//20150328晚,不使用乘号运算符实现两个数相乘
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)

//using namespace std;


int main()
{
    int m, n, t;
    long long ll, absn, absm;
    while (scanf("%d %d", &m, &n) != EOF)
    {
        absn = n; absm = m; ll = 0; t = 0;
        if (m < 0) absm = 0 - absm;
        if (n < 0) absn = 0 - n;
        for (int i = 1; i > 0; i <<= 1)
        {
            if (i&absn)
                ll += absm << t;
            t++;
        }
        if ((!(m < 0 && n < 0)) && (m < 0 || n < 0)) ll = 0 - ll;
        printf("%lld\n", ll);
    }
    return 0;
}

 

posted on 2016-03-28 23:06  鼬与轮回  阅读(492)  评论(0编辑  收藏  举报