在java中使用二进制数 [英] working with binary numbers in java

查看:198
本文介绍了在java中使用二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪一个是在java中使用二进制数的最佳方法。
我需要一种方法来创建一个二进制数组并用它们进行一些计算。
例如,我想X或二进制数的值或乘法矩阵。

I would like to know which one is the best way to work with binary numbers in java. I need a way to create an array of binary numbers and do some calculations with them. For example, I would like to X-or the values or multiply matrix of binary numbers.

问题解决了:
非常感谢所有人信息。

Problem solved: Thanks very much for all the info.

我想我的情况是要使用@Jarrod Roberson提到的BitSet

I think for my case I'm going to use the BitSet mentioned by @Jarrod Roberson

推荐答案

在Java版本7中,您可以通过声明整数并使用 0b 0B :

In Java edition 7, you can simply use binary numbers by declaring ints and preceding your numbers with 0b or 0B:

int x=0b101;
int y=0b110;
int z=x+y;

System.out.println(x + "+" + y + "=" + z);
//5+6=11

/*
* If you want to output in binary format, use Integer.toBinaryString()
*/

System.out.println(Integer.toBinaryString(x) + "+" + Integer.toBinaryString(y)
         + "=" + Integer.toBinaryString(z));
//101+110=1011

这篇关于在java中使用二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆