HDLbits 刷题 --Gatesv

You are given a four-bit input vector in[3:0]. We want to know some relationships between each bit and its neighbour:

  • out_both: Each bit of this output vector should indicate whether both the corresponding input bit and its neighbour to the left (higher index) are '1'. For example, out_both[2] should indicate if in[2] and in[3] are both 1. Since in[3] has no neighbour to the left, the answer is obvious so we don't need to know out_both[3].
  • out_any: Each bit of this output vector should indicate whether any of the corresponding input bit and its neighbour to the right are '1'. For example, out_any[2] should indicate if either in[2] or in[1] are 1. Since in[0] has no neighbour to the right, the answer is obvious so we don't need to know out_any[0].
  • out_different: Each bit of this output vector should indicate whether the corresponding input bit is different from its neighbour to the left. For example, out_different[2] should indicate if in[2] is different from in[3]. For this part, treat the vector as wrapping around, so in[3]'s neighbour to the left is in[0].

译:

        您给出了一个四位输入向量in[3:0]。我们想知道每个位与其相邻位之间的一些关系:

out_both: 此输出向量的每个位应指示相应的输入位及其左侧邻位(较高索引)是否均为'1'。例如,out_both[2]应指示in[2]和in[3]是否都为1。由于in[3]没有左侧邻位,答案是显而易见的,所以我们不需要知道out_both[3]。

out_any: 此输出向量的每个位应指示相应的输入位及其右侧邻位(较低索引)是否有任意一个为'1'。例如,out_any[2]应指示in[2]或in[1]是否有一个为1。由于in[0]没有右侧邻位,答案是显而易见的,所以我们不需要知道out_any[0]。

out_different: 此输出向量的每个位应指示相应的输入位是否与其左侧邻位不同。例如,out_different[2]应指示in[2]是否与in[3]不同。对于此部分,将向量视为环绕,因此in[3]的左侧邻位是in[0]。
 

个人解法:

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    int i;
    always@(*)begin
        for(i=0;i<4;i++)begin
            if (i<3)begin
                out_both[i]=in[i]&in[i+1];
            end
            if (i>0)begin
                out_any[i]=in[i-1]|in[i];
            end
            if (i<3)begin
                out_different[i]=in[i]^in[i+1];
            end                     
        end
        //out_both[2]=0;
      //  out_any[1]=0;
    	out_different[3]=in[3]^in[0];
    end
                
endmodule

官方解法:

module top_module (
	input [3:0] in,
	output [2:0] out_both,
	output [3:1] out_any,
	output [3:0] out_different
);

	// Use bitwise operators and part-select to do the entire calculation in one line of code
	// in[3:1] is this vector:   					 in[3]  in[2]  in[1]
	// in[2:0] is this vector:   					 in[2]  in[1]  in[0]
	// Bitwise-OR produces a 3 bit vector.			   |      |      |
	// Assign this 3-bit result to out_any[3:1]:	o_a[3] o_a[2] o_a[1]

	// Thus, each output bit is the OR of the input bit and its neighbour to the right:
	// e.g., out_any[1] = in[1] | in[0];	
	// Notice how this works even for long vectors.
	assign out_any = in[3:1] | in[2:0];

	assign out_both = in[2:0] & in[3:1];
	
	// XOR 'in' with a vector that is 'in' rotated to the right by 1 position: {in[0], in[3:1]}
	// The rotation is accomplished by using part selects[] and the concatenation operator{}.
	assign out_different = in ^ {in[0], in[3:1]};
	
endmodule

分析:

        1. 可以直接截取数据长度进行处理

        2. 对于旋转操作,可以采用链接符和截取功能,先截取,然后再排序

官方的写法简洁了很多,个人写法还是受C语言影响大些,第一反应选择了for循环,这里是完全用不到的;

运行结果:

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刚及格的陆拾伍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值