1. String类型的二进制(原码)--->反码
public static String InvertedCode(String s){
StringBuilder sr = new StringBuilder(s);
for (int i = 0; i < sr.length(); i++) {
if (sr.substring(i,i+1).equals("0")){
sr.replace(i,i+1,"1");
}else{
sr.replace(i,i+1,"0");
}
}
return sr.toString();
}
2.String类型的二进制(反码)--->补码
public static String Complement(String st) {
StringBuilder sr = new StringBuilder(st);
if (st.substring(st.length() - 1).equals("0")) {
sr.replace(st.length() - 1, st.length(), "1");
return sr.toString();
} else {
sr.replace(st.length() - 1, st.length(), "0");
for (int i = st.length() - 1; i > 0; i--) {
if (st.substring(i - 1, i).equals("0")) {
sr.replace(i - 1, i, "1");
return sr.toString();
} else {
sr.replace(i - 1, i, "0");
}
}
return sr.toString();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)