This Java code finds the two's complement of a binary string. It traverses the string from the end to find the first '1' bit. It then flips all bits from that point to the beginning of the string to get the two's complement. The code demonstrates this by taking the two's complement of the binary string "00000101", printing the result.
This Java code finds the two's complement of a binary string. It traverses the string from the end to find the first '1' bit. It then flips all bits from that point to the beginning of the string to get the two's complement. The code demonstrates this by taking the two's complement of the binary string "00000101", printing the result.
This Java code finds the two's complement of a binary string. It traverses the string from the end to find the first '1' bit. It then flips all bits from that point to the beginning of the string to get the two's complement. The code demonstrates this by taking the two's complement of the binary string "00000101", printing the result.
static String findTwoscomplement(StringBuffer str) { int n = str.length();
// Traverse the string to get first '1' from
// the last of string int i; for (i = n-1 ; i >= 0 ; i--) if (str.charAt(i) == '1') break;
// If there exists no '1' concat 1 at the
// starting of string if (i == -1) return "1" + str;
// Continue traversal after the position of
// first '1' for (int k = i-1 ; k >= 0; k--) { //Just flip the values if (str.charAt(k) == '1') str.replace(k, k+1, "0"); else str.replace(k, k+1, "1"); }
// return the modified string
return str.toString(); } public static void main(String[]args){ StringBuffer str = new StringBuffer("00000101"); System.out.println(findTwoscomplement(str)); } }