バイナリを1つ増やすために使用したコードを変更しています。私は最も簡単な方法は、バイナリを1つから減算することだと決めました。私はちょうどそれを変更する方法を知らない。私は主に動作しますが、0が1から減算されるとき、私はキャリー変数で何をすべきか分かりません。ここで私が持っているものです。整数に変換せずにバイナリ数を1減らす
String decB(String b)
{
String b2 = "0001"; //binary of 1 will be subtracted from the user input binary
String b3 = ""; //result initialized as empty string
int i1 = b.length()-1; //start at the end of the first string
int i2 = b2.length()-1; //start at the end of the second string
int c = 0; //initialize storage as zero. Prevents negative digits.
while (i1 >= 0 && i2 >= 0) //while digits are either one or zero
{
int difference = ((b.charAt(i1)-'0')-(b2.charAt(i2)-'0')+c); //subtracts the binaries
c = difference/2;
b3 = difference%2 + b3;
i1--;
i2--;
}
if (c < 0)
b3 = b3 + c;
return b3;
}
のtwo's complementを追加するために使用したのと同じコード怠惰な使用になりたい場合はだからあなたが「算数」を行うために必要としていますストリング上で直接?この宿題ですか? – markspace
うん。私は本当にここからどこに行くのか分からない。 – Snix
あなたが持っている場合は、手で試してみてください。次に、これらのステップをプログラムで再現してみます。 – markspace