Binary 找到2 1'的组合;是二进制数
我们有一个二进制数,我们需要从给定的数字生成2 1的组合。如果给出这样一个21的组合,我们应该能够产生下一个组合 例如:-Binary 找到2 1'的组合;是二进制数,binary,logic,vhdl,binary-data,digital-logic,Binary,Logic,Vhdl,Binary Data,Digital Logic,我们有一个二进制数,我们需要从给定的数字生成2 1的组合。如果给出这样一个21的组合,我们应该能够产生下一个组合 例如:- Given vector : 10101111 Given combination : 10100000 output : 10001000 Given vector : 10101111 Given combination : 10001000 output : 10000100 Given vector : 10101111 Give
Given vector : 10101111 Given combination : 10100000 output : 10001000
Given vector : 10101111 Given combination : 10001000 output : 10000100
Given vector : 10101111 Given combination : 10000010 output : 10000001
Given vector : 10101111 Given combination : 10000001 output : 00101000
Given vector : 10101111 Given combination : 00101000 output : 00100100
编辑:
一旦第二个1到达给定二进制数中的最后一个1,则第一个1递增(设置为二进制数中的下一个“1”,第二个“1”变为第一个“1”之后的“1”(如例4所示))
这是在硬件中完成的,因此它不应该计算复杂。我们如何用VHDL设计这个模块。以下是一些异步代码,可以完成这项工作:
你的例子没有多大意义。鉴于
10000010
和10000001
几乎相同,它们如何能产生截然不同的100000001
和00101000
作为输出?什么应该10101111
和00000011
作为输出?@JoachimIsaksson:给定的条件是最终的情况。这种情况可以忽略(系统不会将最终组合作为输入)@MarcB:所有输出都是给定二进制数的下一个组合2 1。因此,当第二个“1”是二进制数中的最后一个1时,增加第一个“1”
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity nex2ones is
Port ( vector : in STD_LOGIC_VECTOR (1 to 8);
combo1 : in STD_LOGIC_VECTOR (1 to 8);
combo2 : out STD_LOGIC_VECTOR (1 to 8);
error : out STD_LOGIC);
end nex2ones;
architecture Behavioral of nex2ones is
type int_array_8 is array (1 to 8) of integer range 0 to 8;
begin
process (vector,combo1)
variable ones_ixs : int_array_8;
variable first_combo1_ix : integer range 0 to 8 := 0;
variable second_combo1_ix: integer range 0 to 8 := 0;
variable first_combo1_k : integer range 0 to 9 := 0;
variable second_combo1_k : integer range 0 to 9 := 0;
variable k : integer range 1 to 9;
begin
ones_ixs := (others => 0); -- indices of 1s in vector
combo2 <= (others => '0');
k := 1;
first_combo1_ix := 0;
second_combo1_ix := 0;
first_combo1_k := 0; -- corresponding ptr to ones_ixs
second_combo1_k := 0;
error <= '0';
for j in 1 to 8 loop
if combo1(j) = '1' then
if first_combo1_ix = 0 then
first_combo1_ix := j;
first_combo1_k := k;
else
second_combo1_ix := j;
second_combo1_k := k;
end if;
end if;
if vector(j) = '1' then
ones_ixs(k) := j;
k := k + 1;
end if;
end loop;
if k > 1 then k := k - 1; end if; -- point to last nonzero index
if (first_combo1_ix = 0 or second_combo1_ix = 0)
--or (first_combo1_ix = ones_ixs(k-1) and second_combo1_ix = ones_ixs(k))
or (k < 2) then
error <= '1';
else -- no error proceed
if second_combo1_ix = ones_ixs(k) then -- can't slide 2nd anymore
if (second_combo1_k - first_combo1_k) > 1 then -- is 1st movable
combo2(ones_ixs(first_combo1_k + 1)) <= '1'; -- move 1st
if (second_combo1_k - first_combo1_k) > 2 then -- is 2nd movable
combo2(ones_ixs(first_combo1_k + 2)) <= '1'; -- move 2nd
else
combo2(ones_ixs(second_combo1_k)) <= '1'; -- leave 2nd be
end if;
else
error <= '1'; -- no mas
end if;
else
combo2(ones_ixs(first_combo1_k)) <= '1'; -- leave 1st be
combo2(ones_ixs(second_combo1_k + 1)) <= '1'; -- next
end if;
end if;
end process;
end Behavioral;
ps vector combo1 combo2
error
0 00000000 00000000 00000000 1
100000 10101111 10100000 10001000 0
200000 10101111 10001000 10000100 0
300000 10101111 10000010 10000001 0
400000 10101111 10000001 00101000 0
500000 10101111 00101000 00100100 0
600000 10101111 00100100 00100010 0
700000 10101111 00000011 00000000 1
800000 11001110 00000110 00000000 1
900000 10001110 00001010 00000110 0
1000000 11001110 00001010 00000110 0