COA代码解析 intToBinary() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public String intToBinary (String numStr) { int num = Integer.parseInt(numStr); StringBuilder numBinary = new StringBuilder(Integer.toBinaryString(num)); int len = numBinary.length(); for (int i = 0 ; i < 32 - len; i++) { numBinary.insert(0 , "0" ); } return numBinary.toString(); }
1 2 3 4 5 6 7 8 public String intToBinary (String numStr) { String res = "" ; for (int i = 0 ; i < 32 ; i++) { res += numStr & 1 ; numStr >> 1 ; } return res; }
调用库函数Integer.toBinaryString(num) 将字符串长度补全为32位
binaryToInt() 1 2 3 4 5 6 7 8 9 10 public String binaryToInt (String binStr) { int num = Integer.parseUnsignedInt(binStr, 2 ); return String.valueOf(num); }
调用库函数Integer.parseUnsignedInt(binstr, 2)
decimalToNBCD ()1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public String decimalToNBCD (String decimalStr) { StringBuilder numBCD = new StringBuilder(); if (decimalStr.startsWith("-" )) { numBCD.append("1101" ); decimalStr = decimalStr.substring(1 , decimalStr.length()); } else { numBCD.append("1100" ); } for (int i = 0 ; i < decimalStr.length(); i++) { StringBuilder partBCD = new StringBuilder(Integer.toBinaryString(decimalStr.charAt(i) - 48 )); int len = partBCD.length(); for (int j = 0 ; j < 4 - len; j++) { partBCD.insert(0 , "0" ); } numBCD.append(partBCD); } int len = numBCD.length(); for (int i = 0 ; i < 32 - len; i++) { numBCD.insert(4 , "0" ); } return numBCD.toString(); }
NBCD码:将每一位数转化为4位二进制串并拼接,并在头部添加符号
负号:1101 正号:1100
代码亦可使用键值对(将每一位数字与4位字符串一一对应)直接实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public String decimalToNBCDAno (String decimalStr) { HashMap<Character, String> intToNBCDS; intToNBCDS.append('-' , "1101" ); intToNBCDS.append('0' , "0000" ); intToNBCDS.append('1' , "0001" ); intToNBCDS.append('8' , "1000" ); intToNBCDS.append('9' , "1001" ); StringBuilder res = new StringBuilder(); for (int i = 0 ; i < decimalStr.length(); i++) { res.append(intToNBCDS.get(decimalStr.charAt(i))); } if (!res.startsWith("1101" )) { res.insert(0 , "1100" ); } return res.toString(); }
NBCDToDecimal ()1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public String NBCDToDecimal (String NBCDStr) { StringBuilder numStr = new StringBuilder(); for (int i = NBCDStr.length() - 4 ; i > 3 ; i = i - 4 ) { numStr.insert(0 , Integer.parseInt(NBCDStr.substring(i, i + 4 ), 2 )); } if (NBCDStr.startsWith("1101" )) { numStr.insert(0 , '-' ); } return String.valueOf(num); }
floatToBinary ()1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 public String floatToBinary (String floatStr) { if (Float.parseFloat(floatStr) == Float.POSITIVE_INFINITY) { return "+Inf" ; } else if (Float.parseFloat(floatStr) == Float.NEGATIVE_INFINITY) { return "-Inf" ; } String abs = floatStr; if (floatStr.charAt(0 ) == '-' ) { abs = floatStr.substring(1 ); } String expStr; String sigStr = "" ; int exp; boolean isNormalized = true ; for (exp = -126 ; ; exp++) { if (Math.pow(2 , exp) <= Float.parseFloat(abs) && Float.parseFloat(abs) < Math.pow(2 , exp + 1 )) { break ; } if (exp == 128 ) { exp = -126 ; isNormalized = false ; break ; } } if (!isNormalized) { expStr = "00000000" ; double sig = Float.parseFloat(abs) / Math.pow(2 , exp); for (int i = 0 ; i < 23 ; i++) { sig = sig * 2 ; if (sig >= 1 ) { sigStr += "1" ; sig -= 1 ; } else { sigStr += "0" ; } } if (floatStr.charAt(0 ) == '-' ) { return "1" + expStr + sigStr; } return "0" + expStr + sigStr; } expStr = Integer.toBinaryString(exp + 127 ); int expStrLen = expStr.length(); for (int i = 0 ; i < 8 - expStrLen; i++) { expStr = "0" + expStr; } double sig = Float.parseFloat(abs) / Math.pow(2 , exp) - 1 ; for (int i = 0 ; i < 23 ; i++) { sig = sig * 2 ; if (sig >= 1 ) { sigStr += "1" ; sig -= 1 ; } else { sigStr += "0" ; } } if (floatStr.charAt(0 ) == '-' ) { return "1" + expStr + sigStr; } return "0" + expStr + sigStr; }
binaryToFloat() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public String binaryToFloat (String binStr) { if (binStr.equals("01111111100000000000000000000000" )) { return "+Inf" ; } else if (binStr.equals("11111111100000000000000000000000" )) { return "-Inf" ; } String expStr = binStr.substring(1 , 9 ); boolean isNormalized = true ; if (expStr.equals("00000000" )) { isNormalized = false ; } int exp; double sig = 0 ; double res; if (!isNormalized) { exp = -126 ; for (int i = 0 ; i < 23 ; i++) { if (binStr.charAt(9 + i) == '1' ) { sig += Math.pow(2 , -(i + 1 )); } } res = Math.pow(2 , exp) * sig; if (binStr.charAt(0 ) == '1' ) { return String.valueOf(-res); } return String.valueOf(res); } exp = Integer.parseInt(expStr, 2 ) - 127 ; sig = 1 ; for (int i = 0 ; i < 23 ; i++) { if (binStr.charAt(9 + i) == '1' ) { sig += Math.pow(2 , -(i + 1 )); } } res = Math.pow(2 , exp) * sig; if (binStr.charAt(0 ) == '1' ) { return String.valueOf(-res); } return String.valueOf(res); }
ALU add() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public DataType add (DataType src, DataType dest) { String first = src.toString(); String second = dest.toString(); StringBuilder result = new StringBuilder(); int temp = 0 ; for (int i = 31 ; i >= 0 ; i--) { result.insert(0 , (char ) (((first.charAt(i) - 48 ) + (second.charAt(i) - 48 ) + temp) % 2 + 48 )); temp = ((first.charAt(i) - 48 ) + (second.charAt(i) - 48 ) + temp) / 2 ; } return new DataType(result.toString()); }
sub() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public DataType sub (DataType src, DataType dest) { DataType inSrc = buma(src); return add(inSrc, dest); } public DataType buma (DataType src) { String srcChars = src.toString(); StringBuilder inChars = new StringBuilder(); DataType one = new DataType("00000000000000000000000000000001" ); for (int i = 0 ; i < 32 ; i++) { if (srcChars.charAt(i) == '0' ) { inChars.append("1" ); } else { inChars.append("0" ); } } return add(new DataType(inChars.toString()), one); }
将减数转换为补码直接调用加法即可
mul() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public DataType mul (DataType src, DataType dest) { String destStr = dest.toString(); StringBuilder res = new StringBuilder(); for (int i = 0 ; i < this .length; i++) { res.append(destStr.charAt(i)); } for (int i = this .length; i >= 0 ; i--) { res.insert(0 , "0" ); } destChars += "0" ; for (int i = 0 ; i < this .length; i++) { if (destStr.charAt(this .length - i) - destStr.charAt(this .length - i - 1 ) == 1 ) { res = new StringBuilder(add(new DataType(res.substring(0 , this .length)), src) + res.substring(this .length)); res.insert(0 , "1" ); res.deleteCharAt(res.length() - 1 ); continue ; } if (destStr.charAt(this .length - i) - destStr.charAt(this .length - i - 1 ) == -1 ) { res = new StringBuilder(sub(src, new DataType(res.substring(0 , this .length))) + res.substring(this .length)); } res.insert(0 , "0" ); res.deleteCharAt(res.length() - 1 ); } res.deleteCharAt(res.length() - 1 ); return new DataType(res.substring(this .length)); }
div() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 public DataType div (DataType src, DataType dest) { String divedNum = dest.toString(); String divNum = src.toString(); if (divNum.indexOf('1' ) == -1 ) { throw new ArithmeticException(); } if (divNum.equals(divedNum)) { remainderReg = new DataType("00000000000000000000000000000000" ); return new DataType("00000000000000000000000000000001" ); } if (divedNum.indexOf('1' ) == -1 ) { remainderReg = new DataType("00000000000000000000000000000000" ); return new DataType("00000000000000000000000000000000" ); } if ("10000000000000000000000000000000" .equals(divedNum)) { remainderReg = new DataType("00000000000000000000000000000000" ); return new DataType("10000000000000000000000000000000" ); } char signDivedNum = divedNum.charAt(0 ); char signDivNum = divNum.charAt(0 ); char signReminer = signDivedNum; if (signDivNum != signDivedNum) { if (signDivNum == '1' ) { divNum = abs(src).toString(); return sub(div(new DataType(divNum), dest), new DataType("00000000000000000000000000000000" )); } else { divedNum = abs(dest).toString(); DataType result = sub(div(src, new DataType(divedNum)), new DataType("00000000000000000000000000000000" )); if (!result.toString().contains("1" )) { remainderReg = dest; return result; } remainderReg = sub(remainderReg, new DataType("00000000000000000000000000000000" )); return result; } } else { if (signDivNum == '1' ) { divNum = abs(src).toString(); divedNum = abs(dest).toString(); DataType result = div(new DataType(divNum), new DataType(divedNum)); remainderReg = sub(remainderReg, new DataType("00000000000000000000000000000000" )); return result; } } StringBuilder quotientRemain; quotientRemain = new StringBuilder("00000000000000000000000000000000" + divedNum); quotientRemain = new StringBuilder(sub(src, new DataType(quotientRemain.substring(0 , 32 ))).toString() + quotientRemain.substring(32 )); signReminer = quotientRemain.charAt(0 ); for (int i = 0 ; i < 32 ; i++) { quotientRemain = new StringBuilder(quotientRemain.substring(1 )); if (signReminer == signDivNum) { quotientRemain = new StringBuilder(sub(src, new DataType(quotientRemain.substring(0 , 32 ))).toString() + quotientRemain.substring(32 )); } else { quotientRemain = new StringBuilder(add(src, new DataType(quotientRemain.substring(0 , 32 ))).toString() + quotientRemain.substring(32 )); } signReminer = quotientRemain.charAt(0 ); if (signReminer == signDivNum) { quotientRemain.append("1" ); } else { quotientRemain.append("0" ); } } signReminer = quotientRemain.charAt(0 ); String quotient = quotientRemain.substring(32 ); remainderReg = new DataType(quotientRemain.substring(0 , 32 )); if (signDivedNum != signReminer) { remainderReg = add(remainderReg, src); } if (!quotient.contains("1" )) { remainderReg = dest; } return new DataType(quotient); }
除法部分注意事项:
代码规格化部分逻辑如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 while (隐藏位 == 0 && 阶码 > 0 ) { 尾数左移,阶码减1 ; } while (尾数前27 位不全为0 && 阶码 < 0 ) { 尾数右移,阶码加1 ; } if (阶码上溢) { 将结果置为无穷; } else if (阶码下溢) { 将结果置为0 ; } else if (阶码 == 0 ) { 尾数右移一次化为非规格化数; } else { 此时阶码正常,无需任何操作; }
div() 与mul相比,除阶码加法改为减法、尾数由乘法改为除法以外无区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 public DataType div (DataType src, DataType dest) { String plussedNum = dest.toString(); String plusNum = src.toString(); if (plussedNum.matches(IEEE754Float.NaN_Regular) || plusNum.matches(IEEE754Float.NaN_Regular)) { return new DataType(IEEE754Float.NaN); } if (cornerCheck(divCorner, plussedNum, plusNum) != null ) { return new DataType(Objects.requireNonNull(cornerCheck(divCorner, plussedNum, plusNum))); } String signPlussedNum = plussedNum.substring(0 ,1 ); StringBuilder exponentPlussedNum = new StringBuilder(plussedNum.substring(1 , 9 )); StringBuilder suffixPlussedNum = new StringBuilder(plussedNum.substring(9 )); if (exponentPlussedNum.indexOf("1" ) != -1 ) { suffixPlussedNum.insert(0 , "1" ); } else { suffixPlussedNum.insert(0 , "0" ); exponentPlussedNum.setCharAt(7 , '1' ); } suffixPlussedNum.append("000" ); String signPlusNum = plusNum.substring(0 ,1 ); StringBuilder exponentPlusNum = new StringBuilder(plusNum.substring(1 , 9 )); StringBuilder suffixPlusNum = new StringBuilder(plusNum.substring(9 )); if (exponentPlusNum.indexOf("1" ) != -1 ) { suffixPlusNum.insert(0 , "1" ); } else { suffixPlusNum.insert(0 , "0" ); exponentPlusNum.setCharAt(7 , '1' ); } suffixPlusNum.append("000" ); int exponentNum = Integer.parseInt(exponentPlussedNum.toString(), 2 ) - Integer.parseInt(exponentPlusNum.toString(), 2 ) + 127 ; for (int i = suffixPlusNum.length(); i < 32 ; i++) { suffixPlusNum.insert(0 , "0" ); suffixPlussedNum.insert(0 , "0" ); } StringBuilder suffixResult = new StringBuilder(div(suffixPlusNum.toString(), suffixPlussedNum.toString())); suffixResult = new StringBuilder(suffixResult.substring(0 , 27 )); char signResult; if (signPlussedNum.equals(signPlusNum)) { signResult = '0' ; } else { signResult = '1' ; } while (suffixResult.charAt(0 ) == '0' && exponentNum > 0 ) { suffixResult.append("0" ); suffixResult.deleteCharAt(0 ); exponentNum--; } while (suffixResult.substring(0 , 27 ).contains("1" ) && exponentNum < 0 ) { suffixResult = new StringBuilder(rightShift(suffixResult.toString(), 1 )); exponentNum++; } if (exponentNum > 254 ) { if (signResult == '1' ) { return new DataType(IEEE754Float.N_INF); } else { return new DataType(IEEE754Float.P_INF); } } else if (exponentNum < 0 ) { if (signResult == '0' ) { return new DataType(IEEE754Float.P_ZERO); } else { return new DataType(IEEE754Float.N_ZERO); } } else if (exponentNum == 0 ) { suffixResult = new StringBuilder(rightShift(suffixResult.toString(), 1 )); } StringBuilder exponentResult = new StringBuilder(Integer.toBinaryString(exponentNum)); while (exponentResult.length() > 8 ) { exponentResult.deleteCharAt(0 ); } while (exponentResult.length() < 8 ) { exponentResult.insert(0 , "0" ); } return new DataType(round(signResult, exponentResult.toString(), suffixResult.toString())); }
add() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 public DataType add (DataType src, DataType dest) { String plussedNum = dest.toString(); String plusNum = src.toString(); if (plussedNum.matches(IEEE754Float.NaN_Regular) || plusNum.matches(IEEE754Float.NaN_Regular)) { return new DataType(IEEE754Float.NaN); } if (cornerCheck(addCorner, plussedNum, plusNum) != null ) { return new DataType(Objects.requireNonNull(cornerCheck(addCorner, plussedNum, plusNum))); } if (plussedNum.equals(IEEE754Float.P_ZERO) || plussedNum.equals(IEEE754Float.N_ZERO)) { return src; } if (plusNum.equals(IEEE754Float.P_ZERO) || plusNum.equals(IEEE754Float.N_ZERO)) { return dest; } String signPlussedNum = plussedNum.substring(0 ,1 ); StringBuilder exponentPlussedNum = new StringBuilder(plussedNum.substring(1 , 9 )); StringBuilder suffixPlussedNum = new StringBuilder(plussedNum.substring(9 )); if (exponentPlussedNum.indexOf("1" ) != -1 ) { suffixPlussedNum.insert(0 , "1" ); } else { suffixPlussedNum.insert(0 , "0" ); exponentPlussedNum.setCharAt(7 , '1' ); } suffixPlussedNum.append("000" ); String signPlusNum = plusNum.substring(0 ,1 ); StringBuilder exponentPlusNum = new StringBuilder(plusNum.substring(1 , 9 )); StringBuilder suffixPlusNum = new StringBuilder(plusNum.substring(9 )); if (exponentPlusNum.indexOf("1" ) != -1 ) { suffixPlusNum.insert(0 , "1" ); } else { suffixPlusNum.insert(0 , "0" ); exponentPlusNum.setCharAt(7 , '1' ); } suffixPlusNum.append("000" ); boolean plussedLargerPlus = true ; for (int i = 0 ; i < 8 ; i++) { if (exponentPlussedNum.charAt(i) != exponentPlusNum.charAt(i)) { if (exponentPlussedNum.charAt(i) == '1' ) { break ; } else { plussedLargerPlus = false ; break ; } } } if (plussedLargerPlus) { while (!exponentPlussedNum.toString().equals(exponentPlusNum.toString())) { exponentPlusNum = exponentAdd(exponentPlusNum); suffixPlusNum = new StringBuilder(rightShift(suffixPlusNum.toString(), 1 )); } } else { while (!exponentPlussedNum.toString().equals(exponentPlusNum.toString())) { exponentPlussedNum = exponentAdd(exponentPlussedNum); suffixPlussedNum = new StringBuilder(rightShift(suffixPlussedNum.toString(), 1 )); } } if (suffixPlusNum.indexOf("1" ) == -1 ) { return dest; } if (suffixPlussedNum.indexOf("1" ) == -1 ) { return src; } StringBuilder suffixResult; StringBuilder exponentResult = exponentPlussedNum; if (signPlussedNum.equals(signPlusNum)) { suffixResult = add(suffixPlussedNum, suffixPlusNum, signPlussedNum); } else { suffixResult = sub(suffixPlussedNum, suffixPlusNum, signPlussedNum); } signPlussedNum = String.valueOf(suffixResult.charAt(suffixResult.length() - 1 )); suffixResult = new StringBuilder(suffixResult.substring(0 , suffixResult.length() - 1 )); if (suffixResult.length() > 27 ) { while (suffixResult.length() != 27 ) { suffixResult.deleteCharAt(suffixResult.length()-1 ); exponentResult = exponentAdd(exponentResult); if (exponentResult.indexOf("0" ) == -1 ) { if ("1" .equals(signPlussedNum)) { return new DataType(IEEE754Float.N_INF); } else { return new DataType(IEEE754Float.P_INF); } } } } if (suffixResult.length() < 27 ) { while (suffixResult.length() != 27 ) { suffixResult.append("0" ); exponentResult = exponentSub(exponentResult); if (exponentResult.indexOf("1" ) == -1 ) { suffixResult = new StringBuilder(rightShift(suffixResult.toString(), 1 )); for (int i = suffixResult.length(); i < 27 ; i++) { suffixResult.insert(0 , '0' ); } String temp = (signPlussedNum + exponentResult.toString() + suffixResult.substring(1 , 24 )); if (temp.equals(IEEE754Float.N_ZERO)) { signPlussedNum = "0" ; } return new DataType(round(signPlussedNum.charAt(0 ), exponentResult.toString(), suffixResult.toString())); } } } while (suffixResult.charAt(0 ) != '1' ) { if ("00000001" .equals(exponentResult.toString()) && suffixResult.charAt(0 ) == '0' ) { exponentResult = exponentSub(exponentResult); break ; } suffixResult.append("0" ); suffixResult.deleteCharAt(0 ); exponentResult = exponentSub(exponentResult); } String temp = (signPlussedNum + exponentResult.toString() + suffixResult.substring(1 , 24 )); if (temp.equals(IEEE754Float.N_ZERO)) { signPlussedNum = "0" ; } return new DataType(round(signPlussedNum.charAt(0 ), exponentResult.toString(), suffixResult.toString())); }
sub() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public DataType sub (DataType src, DataType dest) { StringBuilder srcNum = new StringBuilder(src.toString()); if (srcNum.charAt(0 ) == '1' ) { srcNum.setCharAt(0 , '0' ); } else { srcNum.setCharAt(0 , '1' ); } src = new DataType(srcNum.toString()); String plussedNum = dest.toString(); String plusNum = src.toString(); if (plussedNum.matches(IEEE754Float.NaN_Regular) || plusNum.matches(IEEE754Float.NaN_Regular)) { return new DataType(IEEE754Float.NaN); } if (cornerCheck(subCorner, plussedNum, plusNum) != null ) { return new DataType(Objects.requireNonNull(cornerCheck(subCorner, plussedNum, plusNum))); } return add(src, dest); }
NBCDU add() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 int c = 0 ; DataType add (DataType src, DataType dest) { c = 0 ; int len = 32 ; String plussedNum = dest.toString(); String plusNum = src.toString(); if (!plussedNum.substring(4 ).contains("1" )) { return src; } if (!plusNum.substring(4 ).contains("1" )) { return dest; } boolean ifSub = false ; String sign = "1100" ; String signPlussedNum = plussedNum.substring(0 , 4 ); String signPlusNum = plusNum.substring(0 , 4 ); plussedNum = plussedNum.substring(4 ); plusNum = plusNum.substring(4 ); if (signPlussedNum.equals(signPlusNum)) { if ("1101" .equals(signPlusNum)) { sign = "1101" ; } } else { ifSub = true ; if ("1101" .equals(signPlusNum)) { plusNum = invert(plusNum); / } if ("1101" .equals(signPlussedNum)) { plussedNum = invert(plussedNum); } } StringBuilder result = new StringBuilder(); for (int i = len - 8 ; i >= 0 ; i-= 4 ) { int temp = Integer.parseInt(plussedNum.substring(i, i + 4 ), 2 ) + Integer.parseInt(plusNum.substring(i, i + 4 ), 2 ) + c; c = 0 ; if (temp > 9 ) { temp = temp + 6 - 16 ; c++; } String tempStr = Integer.toBinaryString(temp); tempStr = repeat('0' , 4 - tempStr.length()) + tempStr; result.insert(0 , tempStr); } if (ifSub) { if (c == 0 ) { result = new StringBuilder(invert(result.toString())); if ("1101" .equals(sign)) { sign = "1100" ; } else { sign = "1101" ; } } } else { if (c == 1 ) { result.insert(0 , "0001" ); } } if (result.length() > 28 ) { while (result.length() > 28 ) { result.deleteCharAt(0 ); } } result.insert(0 , repeat('0' , len - 4 - result.length())); c = 0 ; StringBuilder res = new StringBuilder(); for (int i = len - 8 ; i >= 0 ; i-= 4 ) { int temp = Integer.parseInt(result.substring(i, i + 4 ), 2 ) + c; c = 0 ; if (temp > 9 ) { temp = temp + 6 - 16 ; c++; } String tempStr = Integer.toBinaryString(temp); tempStr = repeat('0' , 4 - tempStr.length()) + tempStr; res.insert(0 , tempStr); } res.insert(0 , sign); if (!res.substring(4 ).contains("1" )) { res.setCharAt(3 , '0' ); } return new DataType(res.toString()); }
sub() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 DataType sub (DataType src, DataType dest) { StringBuilder srcBuilder = new StringBuilder(src.toString()); if (!srcBuilder.substring(4 ).contains("1" )) { return add(src, dest); } if (srcBuilder.charAt(3 ) == '0' ) { srcBuilder.setCharAt(3 , '1' ); } else { srcBuilder.setCharAt(3 , '0' ); } return add(new DataType(srcBuilder.toString()), dest); } String oneAdder (String src) { StringBuilder result = new StringBuilder(src); int len = result.length(); if (result.charAt(len - 1 ) == '0' ) { result.setCharAt(len - 1 , '1' ); return result.toString(); } else { for (int i = src.length() - 1 ; i >= 0 ; i--) { if (result.charAt(i) == '0' ) { result.setCharAt(i, '1' ); return result.toString(); } else { result.setCharAt(i, '0' ); } } } return result.toString(); } String repeat (int n) { String result = "" ; for (int i = 0 ; i < n; i++) { result += '0' ; } return result; } String invert (String src) { HashMap<String, String> buma = new HashMap<>(); buma.put("0000" , "1001" ); buma.put("0001" , "1000" ); buma.put("0010" , "0111" ); buma.put("0011" , "0110" ); buma.put("0100" , "0101" ); buma.put("0101" , "0100" ); buma.put("0110" , "0011" ); buma.put("0111" , "0010" ); buma.put("1000" , "0001" ); buma.put("1001" , "0000" ); StringBuilder result = new StringBuilder(); for (int i = src.length(); i > 0 ; i -= 4 ) { if (i == src.length()) { String temp = buma.get(src.substring(i-4 , i)).toString(); temp = oneAdder(temp); result.insert(0 , temp); continue ; } result.insert(0 , buma.get(src.substring(i-4 , i))); } return result.toString(); }
CRC calculate() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public static char [] Calculate(char [] data, String polynomial) { int dataLen = data.length; int polyLen = polynomial.length(); char [] dataChar = new char [dataLen + polyLen - 1 ]; for (int i = 0 ; i < dataLen; i++) { dataChar[i] = data[i]; } for (int i = dataLen; i < dataChar.length; i++) { dataChar[i] = '0' ; } ArrayList<Character> result = new ArrayList<>(); for (int j = 0 ; j < polyLen; j++) { result.add(oplus(dataChar[j], '0' )); } ArrayList<Character> temp = new ArrayList<>(); for (int i = 0 ; i < dataLen; i++) { if (result.get(0 ) == polynomial.charAt(0 )) { for (int j = 1 ; j < polyLen; j++) { temp.add(oplus(result.get(j), polynomial.charAt(j))); } } else { for (int j = 1 ; j < polyLen; j++) { temp.add(oplus(result.get(j), '0' )); } } if (i != dataLen - 1 ) { temp.add(dataChar[i + polyLen]); } result = new ArrayList<>(temp); temp.clear(); } char [] resultChar = new char [result.size()]; for (int i = 0 ; i < resultChar.length; i++) { resultChar[i] = result.get(i); } return resultChar; }
check() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static char [] Check ( char [] data, String polynomial,char [] CheckCode) { int dataLen = data.length; int polyLen = polynomial.length(); char [] dataChar = new char [dataLen + polyLen - 1 ]; for (int i = 0 ; i < dataLen; i++) { dataChar[i] = data[i]; } for (int i = 0 ; i < dataChar.length - dataLen; i++) { dataChar[i + dataLen] = CheckCode[i]; } ArrayList<Character> result = new ArrayList<>(); for (int j = 0 ; j < polyLen; j++) { result.add(dataChar[j]); } ArrayList<Character> temp = new ArrayList<>(); for (int i = 0 ; i < dataLen; i++) { if (result.get(0 ) == polynomial.charAt(0 )) { for (int j = 1 ; j < polyLen; j++) { temp.add(oplus(result.get(j), polynomial.charAt(j))); } } else { for (int j = 1 ; j < polyLen; j++) { temp.add(oplus(result.get(j), '0' )); } } if (i != dataLen - 1 ) { temp.add(dataChar[i + polyLen]); } result = new ArrayList<>(temp); temp.clear(); } char [] resultChar = new char [result.size()]; for (int i = 0 ; i < resultChar.length; i++) { resultChar[i] = result.get(i); } return resultChar; } private static char oplus ( char a, char b) { if (a == b) { return '0' ; } else { return '1' ; } }