Commit 469df11b authored by rokka's avatar rokka

finised 4.1, added 4.2

parent 74e01671
......@@ -61,7 +61,7 @@ String toEightBitBinaryString(int n) {
String resultBinaryString = "";
int numberOfBits = 8 - 1;
if (intToConvert < 1) {
if (intToConvert < 0) {
System.out.println("\"You can't live in that negative way. Make room for the postive in your life.\"");
return resultBinaryString;
}
......@@ -112,15 +112,23 @@ String flipBinaryString(String s) {
}
String toTwosComplement(byte n) {
if ( n < -129 || n > 128) {
System.out.println("Please respect my boundaries. ;>");
return "";
}
// @toDO check why n = (byte) 128 falls threw
// if ( n < (byte) -127 || n > (byte) 128 {
// System.out.println("Please respect my boundaries. ;>");
// return "";
// }
if (n < 0) {
return toEightBitBinaryString(eightBitToDecimal(flipBinaryString(toEightBitBinaryString(-1 * n))) + 1);
return toEightBitBinaryString(eightBitBinaryStringToDecimal(flipBinaryString(toEightBitBinaryString(-1 * n))) + 1);
} else {
return toEightBitBinaryString(n);
}
}
void testToTwosComplement() {
for(int i = -127; i < 129; i++) {
String myTwosComplement = toTwosComplement((byte) i);
String preDefinedMethod = Integer.toBinaryString((i & 0xFF) + 256).substring(1);
System.out.print(String.valueOf(i) + " " + String.valueOf(myTwosComplement.equals(preDefinedMethod) ? "OK " : "FAILED "));
}
}
boolean isOnStick(byte stick, int pos) {
return (1 == ((stick >> pos - 1) & 1)) ? true : false;
}
void printStick(byte stick) {
System.out.println(String.format("%8s", Integer.toBinaryString(stick)).replace(' ', '0'));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment