当一个乘数是常数时,乘法可以通过移位相加或移位相减实现。移位操作不需要使用逻辑门,只需要改变连线方式即可实现。
乘数为2的整数次幂时:
wire [7:0] a1;
wire [12:0] b1;
assign b1 = a1 << 1 = {4'b0000,a1[7:0],1'b0};
assign b1 = a1 << 2 = {3'b000,a1[7:0],2'b0};
assign b1 = a1 << 3 = {2'b00,a1[7:0],3'b0};
乘数为非2的整数次幂时:
wire [7:0] a1;
wire [12:0] b1;
乘以9时:
assign b1 = a1*9 = (a1*8)+a1
=(a1<<3)+a1
乘以10时
assign b1 = a1*10 = (a1*8)+a1*2
=(a1<<3)+(a1<<1)
乘以14时
assign b1 = a1*14 = (a1*8)+ a1*4 + a1*2
=(a1<<3)+(a1<<2)+ (a1<<1)