WooKinson

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

  输入两个无符号整数x, y, 用位操作实现无符号整数的乘法运算。不用考虑整数的溢出。
输入:
  235 657
输出:
  154395
题目描述

 

代码如下:

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main(void)
 6 {
 7     int i,res=0;
 8     unsigned int x,y;
 9     scanf("%d%d",&x,&y);
10     for (i=0 ; i<32 ; i++)    //32位长度为满足测试数据,大小可根据实际修改 
11     {
12         if ((y&1) == 1)    //y的最低位是否为1 
13         {
14             res += x;    //计算相乘的结果 
15         }
16         x <<= 1;
17         y >>= 1;     
18     }
19     printf("%d",res);
20     return 0;
21 }
C解法

 

 

解题思路:

位操作相乘参考:https://blog.csdn.net/luolan9611/article/details/81772481

 

posted on 2019-01-31 23:38  WooKinson  阅读(338)  评论(0编辑  收藏  举报