Skip to content

Instantly share code, notes, and snippets.

@tasdemirbahadir
Created June 6, 2020 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasdemirbahadir/2bafcf479b5a895ab9d2c4ea6e2cd55b to your computer and use it in GitHub Desktop.
Save tasdemirbahadir/2bafcf479b5a895ab9d2c4ea6e2cd55b to your computer and use it in GitHub Desktop.
Substract 1 from binary data. For example "00000011" - 1 -> "00000010"
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