From d7c0052b823d0a831443fa1bd16b028b41271f64 Mon Sep 17 00:00:00 2001 From: hneemann Date: Tue, 9 Aug 2016 09:55:32 +0200 Subject: [PATCH] added bit count element to gui --- .../digital/core/arithmetic/BitCount.java | 6 +++--- .../digital/draw/library/ElementLibrary.java | 1 + src/main/resources/lang/lang_de.xml | 4 ++++ src/main/resources/lang/lang_en.xml | 4 ++++ .../digital/core/arithmetic/BitCountTest.java | 17 +++++++++++++++-- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/neemann/digital/core/arithmetic/BitCount.java b/src/main/java/de/neemann/digital/core/arithmetic/BitCount.java index 5d7876405..e7088f3f1 100644 --- a/src/main/java/de/neemann/digital/core/arithmetic/BitCount.java +++ b/src/main/java/de/neemann/digital/core/arithmetic/BitCount.java @@ -12,7 +12,7 @@ import de.neemann.digital.core.element.Keys; import static de.neemann.digital.core.element.PinInfo.input; /** - * Negation, twos complement + * Bit count * * @author hneemann */ @@ -38,8 +38,8 @@ public class BitCount extends Node implements Element { public BitCount(ElementAttributes attributes) { inBits = attributes.get(Keys.BITS); int outBits = 1; - while (outBits < inBits) - outBits *= 2; + while ((1 << outBits) <= inBits) + outBits++; output = new ObservableValue("out", outBits); } diff --git a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java index 981ca75bb..4b3de0558 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java @@ -95,6 +95,7 @@ public class ElementLibrary implements Iterable add(Mul.DESCRIPTION, menu); add(Comparator.DESCRIPTION, menu); add(Neg.DESCRIPTION, menu); + add(BitCount.DESCRIPTION, menu); menu = Lang.get("lib_test"); add(TestCaseElement.TESTCASEDESCRIPTION, menu); diff --git a/src/main/resources/lang/lang_de.xml b/src/main/resources/lang/lang_de.xml index 023151749..f1d4eb5e0 100644 --- a/src/main/resources/lang/lang_de.xml +++ b/src/main/resources/lang/lang_de.xml @@ -61,6 +61,10 @@ Auf diese Weise kann jedes kombinatorische Gatter erzeugt werden. Nicht Und Nicht Oder Nicht + Negation + Negation im 2-er Komplement + Bitzähler + Gibt die Anzahl der 1-Bits im Eingangswert aus. Oder Ausgang Ein Ausgang der genutzt werden kann, um eine Verbindung zu einem eingebettetem Element herzustellen. diff --git a/src/main/resources/lang/lang_en.xml b/src/main/resources/lang/lang_en.xml index 14f876696..aca54b8a7 100644 --- a/src/main/resources/lang/lang_en.xml +++ b/src/main/resources/lang/lang_en.xml @@ -61,6 +61,10 @@ So this gate can emulate every combinatorial gate. NAnd NOr Not + Negation + Negation in the 2th complement + Bit counter + Returns the number of 1-bits in the input value. Or Out A output which can be used to connect the circuit if it is included in an other circuit. diff --git a/src/test/java/de/neemann/digital/core/arithmetic/BitCountTest.java b/src/test/java/de/neemann/digital/core/arithmetic/BitCountTest.java index 8d555daf8..a2ef80a7b 100644 --- a/src/test/java/de/neemann/digital/core/arithmetic/BitCountTest.java +++ b/src/test/java/de/neemann/digital/core/arithmetic/BitCountTest.java @@ -3,6 +3,7 @@ package de.neemann.digital.core.arithmetic; import de.neemann.digital.TestExecuter; import de.neemann.digital.core.Model; import de.neemann.digital.core.ObservableValue; +import de.neemann.digital.core.ObservableValues; import de.neemann.digital.core.element.ElementAttributes; import junit.framework.TestCase; @@ -11,9 +12,9 @@ import junit.framework.TestCase; */ public class BitCountTest extends TestCase { + public void testBitCount() throws Exception { ObservableValue a = new ObservableValue("a", 6); - ObservableValue b = new ObservableValue("b", 3); Model model = new Model(); @@ -21,7 +22,11 @@ public class BitCountTest extends TestCase { node.setInputs(a.asList()); model.add(node); - TestExecuter sc = new TestExecuter(model).setInputs(a).setOutputs(node.getOutputs()); + ObservableValues outputs = node.getOutputs(); + assertEquals(1, outputs.size()); + assertEquals(3, outputs.get(0).getBits()); + + TestExecuter sc = new TestExecuter(model).setInputs(a).setOutputs(outputs); sc.check(0, 0); sc.check(1, 1); sc.check(2, 1); @@ -39,4 +44,12 @@ public class BitCountTest extends TestCase { } + public void testBits() { + assertEquals(1, new BitCount(new ElementAttributes().setBits(1)).getOutputs().get(0).getBits()); + assertEquals(2, new BitCount(new ElementAttributes().setBits(2)).getOutputs().get(0).getBits()); + assertEquals(3, new BitCount(new ElementAttributes().setBits(6)).getOutputs().get(0).getBits()); + assertEquals(3, new BitCount(new ElementAttributes().setBits(7)).getOutputs().get(0).getBits()); + assertEquals(4, new BitCount(new ElementAttributes().setBits(8)).getOutputs().get(0).getBits()); + } + } \ No newline at end of file