1

2進数を変更して、1と0を逆にするにはどうすればよいでしょうか。整数を2進数に変更する方法をすでに知っています

Example 1: 5 as a parameter would return 2
Steps: 5 as a binary is 101
          The complement is 010
          010 as an integer is 2

整数を2進数に変更するコード

import java.io.*;
public class DecimalToBinary{
  public static void main(String args[]) throws IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter the decimal value:");
  String hex = bf.readLine();

  int i = Integer.parseInt(hex);  


  String bynumber = Integer.toBinaryString(i);


  System.out.println("Binary: " + bynumber);
  }
} 

誰かコードが助けてくれたら、ありがとう!

4

3 に答える 3

4

明示的にバイナリに変換する必要はありません。これにはビット演算子を使用できます。

于 2012-04-08T22:48:32.040 に答える
3

ビット単位のnot関数を使用します~

int num = 0b10101010;
System.out.println(Integer.toBinaryString(num));         // 10101010
System.out.println(Integer.toBinaryString((byte) ~num)); //  1010101 (note the absent leading zero)
于 2012-04-08T22:48:51.373 に答える
3
int i = Integer.parseInt(numString);
i = ~i;

それはそれをする必要があります。

于 2012-04-08T22:51:30.327 に答える