题解 | #位拆分与运算#
位拆分与运算
https://www.nowcoder.com/practice/1649582a755a4fabb9763d07e62a9752
`timescale 1ns/1ns module data_cal( input clk, input rst, input [15:0]d, input [1:0]sel, output [4:0]out, output validout ); //*************code***********// wire [15:0]d0; assign validout = (sel == 2'd0) ? 0 : 1; assign d0 = (validout == 0) ? d : d0; assign out = (validout == 0) ? 0 : (sel == 2'd1) ? d0[3:0] + d0[7:4] : (sel == 2'd2) ? d0[3:0] +d0[11:8] : d0[3:0] +d0[15:12]; //*************code***********// endmodule
寄存一下输入的d值,随后使用三目运算符进行后续的sel处理