ConvertToBinary
Zur Navigation springen
Zur Suche springen
Beschreibung
Die Funktion convertToBinary wandelt Ganzzahlen und Gleitkommazahlen in ihre entsprechende Binärdarstellung um. Die Rückgabe ist eine Zeichenkette.
Negative Gleitkommazahlen werden nach dem IEEE_754 Standard konvertiert, d.h. es gibt immer 64 Stellen. Negative Ganzzahlen werden immer mit 32 Stellen dargestellt.
Syntax
convertToBinary(Ganzzahl zahl)
Parameter
- zahl - die Dezimaldarstellung der Zahl, die in Binär umgewandelt werden soll
Return Value
- Gibt eine Ganzzahl zurück
Beispiele
Standardfälle
convertToBinary(15) --> returns '1111' convertToBinary(3) --> returns '11' convertToBinary(-3) --> returns '11111111111111111111111111111101' convertToBinary(1.2) --> returns '11111111110011001100110011001100110011001100110011001100110011' convertToBinary(-1.2) --> returns '1011111111110011001100110011001100110011001100110011001100110011'
Benutzung von Variablen
convertToBinary('[pos=1]') convertToBinary('[var=a]')
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(2, OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
exerciseVariableMap.put("b", OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
}
@Test
public void testConvertToBinary1() {
assertEquals(OMCreator.createOMSTR("10"),
Evaluator.evaluate("convertToBinary(2)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinary2() {
assertEquals(OMCreator.createOMSTR("1101101111"),
Evaluator.evaluate("convertToBinary(879)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinary3() {
assertNotEquals(OMCreator.createOMSTR("-11"),
Evaluator.evaluate("convertToBinary(-3)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithInput1() {
assertEquals(OMCreator.createOMSTR("111"),
Evaluator.evaluate("convertToBinary([pos=2])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithInput2() {
assertEquals(OMCreator.createOMSTR("0"),
Evaluator.evaluate("convertToBinary([pos=1])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithVariables1() {
assertEquals(OMCreator.createOMSTR("0"),
Evaluator.evaluate("convertToBinary([var=a])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithVariables2() {
assertEquals(OMCreator.createOMSTR("111"),
Evaluator.evaluate("convertToBinary([var=b])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testConvertToBinaryWithWrongInputCharacter() {
Evaluator.evaluate("convertToBinary(a)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithTwoArguments() {
Evaluator.evaluate("convertToBinary(3, 2)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithThreeArguments() {
Evaluator.evaluate("convertToBinary(1, 2, 3)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testConvertToBinaryWithMissingExcerciseVariable() {
Evaluator.evaluate("convertToBinary([var=j])", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testConvertToBinaryWithMissingInput() {
Evaluator.evaluate("convertToBinary([pos=42])", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- Für negative Zahlen wird das Zweierkomplement zurückgegeben.