Created
June 6, 2020 07:54
-
-
Save tasdemirbahadir/a0739482453e34a4287ee90259963f14 to your computer and use it in GitHub Desktop.
Add 1 to binary data. Fro example "00000010" + 1 -> "00000011"
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 add1(String binVal) { | |
StringBuilder sb = new StringBuilder(); | |
int remainder = 0; | |
for (int i = binVal.length() - 1; i >= 0; i--) { | |
if (binVal.charAt(i) == '1') { | |
sb.insert(0, '0'); | |
remainder = 1; | |
} else { | |
sb.insert(0, '1'); | |
remainder = 0; | |
} | |
if (remainder == 0) { | |
sb.insert(0, binVal.substring(0, i)); | |
break; | |
} | |
} | |
if (remainder == 1) { | |
sb.insert(0, '1'); | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment