class KeisanTaro { // 関数をクラスにかこむ void TwosComplement(int[] BIT) { int SW = 0; for(int i=0; i<8; i++){ if(BIT[i]==1){ if(SW==0){ SW = 1; }else{ BIT[i] = 0; } }else{ if(SW != 0){ BIT[i] = 1; } } } } } public class drvTwosComplement01 { public static void main(String[] args) { KeisanTaro taroBunshin = new KeisanTaro(); // クラスからインスタンス // を作成 int BIT[] = new int[8]; // 1010 1000 を代入 BIT[0] = 0; BIT[1] = 0; BIT[2] = 0; BIT[3] = 1; BIT[4] = 0; BIT[5] = 1; BIT[6] = 0; BIT[7] = 1; for(int i=7; i>=0; i--){ System.out.print(BIT[i]); } System.out.println(""); taroBunshin.TwosComplement(BIT); // インスタンス(分身)が関数を実行 for(int i=7; i>=0; i--){ System.out.print(BIT[i]); } } }