gpt4 book ai didi

Java : Changing a binary number bits 0's to 1' s and 1's to 0' s

转载 作者:行者123 更新时间:2023-11-29 09:56:23 25 4
gpt4 key购买 nike

我想知道如何更改二进制数并反转 1 和 0?我已经知道如何将整数转换为二进制数了

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

将整数转换为二进制数的代码

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);
}
}

如果有哪位代码请帮忙,谢谢!

最佳答案

您不需要明确地将其转换为二进制。您可以使用 bitwise operators为此。

关于Java : Changing a binary number bits 0's to 1' s and 1's to 0' s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com