十进制到二进制的转换
我想将十进制数转换为二进制数。我想将它们存储在一个数组中。 首先,我需要创建一个具有一定长度的数组,以便可以存储二进制数。之后我执行转换,我是这样做的:
public class Aufg3 {
public static void main(String[] args) {
int[] test = decToBin(12, getBinArray(12));
for(int i = 0; i < test.length; i++){
System.out.println(test[i]);
}
}
public static int[] getBinArray(int number){
int res = number, length = 0;
while(res != 0){
res /= 2;
length++;
}
return new int[length];
}
public static int[] decToBin(int number, int[] array){
int res = number, k = array.length-1;
while(res != 0){
if(res%2 == 0){
array[k] = 0;
}else{
array[k] = 1;
}
k--;
res /= 2;
}
return array;
}
}
有什么需要改进的吗?输入 12 时应打印 1100。
I want to convert decimal numbers to binary numbers. I want to store them in an array.
First I need to create an array that has a certain length so that I can store the binary numbers. After that I perform the conversion, here is how I do it:
public class Aufg3 {
public static void main(String[] args) {
int[] test = decToBin(12, getBinArray(12));
for(int i = 0; i < test.length; i++){
System.out.println(test[i]);
}
}
public static int[] getBinArray(int number){
int res = number, length = 0;
while(res != 0){
res /= 2;
length++;
}
return new int[length];
}
public static int[] decToBin(int number, int[] array){
int res = number, k = array.length-1;
while(res != 0){
if(res%2 == 0){
array[k] = 0;
}else{
array[k] = 1;
}
k--;
res /= 2;
}
return array;
}
}
Is there anything to improve? It should print 1100 for input of 12.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接使用 Integer类的toBinaryString方法:
Why not just use the toBinaryString method of the Integer class:
我假设您想编写自己的代码——否则使用标准 Java 库中的方法就可以直接完成。
一些快速评论:
res
临时变量。直接处理number
(请记住,Java 按值传递参数)。number >>>= 1
而不是number /= 2
),尽管编译器应该能够对此进行优化array[k] = number & ,请避免使用
decToBin
中的模数。 1;decToBin
调用getBinArray
呢?然后,您可以仅使用一个参数(要转换的值)调用 decToBin。这是代码的稍微优化版本:
I assume you want to write your own code -- otherwise this is straightforward to do using methods from the standard Java library.
Some quick comments:
res
temp vars. Work directly onnumber
(remember that Java passes parameters by value).number >>>= 1
instead ofnumber /= 2
), although the compiler should be able to optimize this anywaydecToBin
if you just doarray[k] = number & 1;
getBinArray
fromdecToBin
directly? Then you can calldecToBin
with only one arg -- the value to convert.Here is a slightly optimized version of your code:
如果这不是家庭作业,则无需自己做。下面的代码应该可以工作:
可能有更有效的方法,但这肯定是非常可读和可维护的。
If this isn't homework, no need to do it yourself. The following code should work:
There might be more efficient ways, but this is certainly very readable and maintainable.
有一些小事情可以改进:
int
转换为int[]
。在当前代码中,您必须提及12
两次,这很糟糕。do { ... } while (number != 0)
循环。否则数字0
将由空数组表示。x >>> 1
而不是x / 2
,因为它可以正确处理负数。binToDec(decToBin(12, ...)) == 12
。getBinArray
方法不应该是public
,因为它只是一个辅助方法。您可以将public
替换为private
,也可以仅删除public
。There are some small things that you can improve:
int
to anint[]
. In the current code you have to mention the12
two times, which is bad.do { ... } while (number != 0)
loop. Otherwise the number0
will be represented by an empty array.x >>> 1
instead ofx / 2
, since that handles negative numbers correctly.int
. Then you can check thatbinToDec(decToBin(12, ...)) == 12
.getBinArray
should not bepublic
, since it is only a helper method. You can either replace thepublic
withprivate
or just remove thepublic
.