1 yuan fan code code code bu
Achieve certification program
package complement; import java.util.Scanner; public class complement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个(-127)-128的整数a"); int Originala; int Originalb; while (true) { Originala = sc.nextInt(); if (Originala >= -127 && Originala <= 128) { System.out.println ( "A is the original code:" + GetBinary (Originala)); IF (Originala> 0 ) { System.out.println ( "A code counter is:" + GetBinary (Originala)); the System .out.println ( "a's complement is:" + GetBinary (Originala)); } the else { System.out.println ( "a code counter is:" + getReverse (GetBinary (Originala))); the System.out .println ( "a's complement is:" + getComplement (getReverse (GetBinary (Originala)))); } BREAK ; } The else { System.out.println ( "out of range, please re-enter" ); } } System.out.println ( "Enter a (-127) -128 integers A" ); the while ( to true ) { Originalb = sc.nextInt (); IF (Originalb> = -127 && Originalb <= 128 ) { System.out.println ( "B is the original code:" + GetBinary (Originalb)); IF (Originalb> 0 ) { System.out.println ( "Code B counter is:" + GetBinary (Originalb)); System.out.println ( "B's complement is:" + GetBinary (Originalb)); } the else { System.out.println ( "inverse code B is:" + getReverse (GetBinary (Originalb))); System.out.println ( "B's complement is:" + getComplement (getReverse (GetBinary (Originalb)))); } BREAK ; } the else { System.out.println ( "out of range, please re-enter" ); } } String ca=getComplement(getReverse(getBinary(Originala))); String cb=getComplement(getReverse(getBinary(Originalb))); int [] complementa=new int[8]; int [] complementb=new int[8]; for(int i=0;i<8;i++) { complementa[i]=Integer.parseInt(ca.charAt(i)+""); complementb[i]=Integer.parseInt(cb.charAt(i)+""); } System.out.println("按位与的结果:"); System.out.println(getBinary(Originala&Originalb)); System.out.println("按位异或的结果:"); System.out.println(getBinary(Originala^Originalb)); } public static String getBinary(int k) { String str = ""; if (k == 0) return "00000000"; else { int flag = k > 0 ? 1 : 0; k = k > 0 ? k : (-k); while (k != 0) { str = k % 2 + str; k = k / 2; } int in = Integer.parseInt(str); if (flag == 1) { str = String.format("%08d", in); } else { str = "1" + String.format("%07d", in); } return str; } } public static String getReverse(String str) { String str2 = ""; for (int i = 1; i < str.length(); i++) { if (str.charAt(i) == '1') { str2 = str2 + "0"; } else if (str.charAt(i) == '0') { str2 = str2 + "1"; } } return "1" + str2; } public static String getComplement(String str) { if ("11111111".equals(str)) return "00000000" ; the else { int index = 0 ; String str2 = "" ; int In Flag = (the Integer.parseInt (str.charAt (. 7) + "") +. 1) == 2. 1: 0;? // . 1 represents binary 0 indicates no carry IF (in Flag ==. 1 ) { for ( int I = str.length () -. 1; I> = 0; i-- ) { IF (in Flag ==. 1) // need to carry it { { IF (the Integer.parseInt (str.charAt (I) + "") +. 1 == 2 ) { str2 = "0" + str2; flag = 1; } else if (Integer.parseInt(str.charAt(i) + "") + 1 == 1) { str2 = "1" + str2; index = i; break; } } } str2 = str.substring(0, index) + str2; } else { str2 = str.substring(0, 7) + "1"; } return str2; } } }
2. pingbi principle variables of the same name
Output
3.
byte (8), short (16), int (32), long (64) floating point: float (32), double (64)
A higher accuracy of the conversion to a lower precision data type without loss of accuracy, the loss of precision to a lower precision high precision data type conversion will
4
Float in the computer storage mechanism
5 string concatenation