Created
June 6, 2020 07:57
-
-
Save tasdemirbahadir/2bafcf479b5a895ab9d2c4ea6e2cd55b to your computer and use it in GitHub Desktop.
Substract 1 from binary data. For example "00000011" - 1 -> "00000010"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String substract1(String binVal) { | |
StringBuilder sb = new StringBuilder(); | |
int remainder = 0; | |
for (int i = binVal.length() - 1; i >= 0; i--) { | |
if (binVal.charAt(i) == '0') { | |
sb.insert(0, '1'); | |
remainder = 1; | |
} else if (i > 0) { | |
sb.insert(0, '0'); | |
remainder = 0; | |
} | |
if (remainder == 0) { | |
sb.insert(0, binVal.substring(0, i)); | |
break; | |
} | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment