Miguel Oliveira
Miguel Oliveira

Reputation: 291

working with binary numbers in java

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.

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

Upvotes: 28

Views: 103898

Answers (5)

ewok
ewok

Reputation: 21503

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

Upvotes: 55

Renato
Renato

Reputation: 846

There's a difference between the number itself and it's representation in the language. For instance, "0xD" (radix 16), "13" (radix 10), "015" (radix 8) and "b1101" (radix 2) are four different representations referring to the same number.

That said, you can use the "int" primitive data type in the Java language to represent any binary number (as well as any number in any radix), but only in Java 7 you are able to use a binary literal as you were previously able to use the octal (0) and hexa (0x) literals to represent those numbers, if I understood correctly your question.

Upvotes: 3

erickson
erickson

Reputation: 269857

I've never seen a computer that uses anything but binary numbers.

The XOR operator in Java is ^. For example, 5 ^ 3 = 6. The default radix for most number-to-string conversions is 10, but there are several methods which allow you to specify another base, like 2:

System.out.println(Integer.toString(5 ^ 3, 2));

If you are using Java 7, you can use binary literals in your source code (in addition to the decimal, hexadecimal, and octal forms previously supported).

Upvotes: 0

user177800
user177800

Reputation:

What you are probably looking for is the BitSet class.

This class implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.

By default, all bits in the set initially have the value false.

Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set relates to logical length of a bit set and is defined independently of implementation.

Unless otherwise noted, passing a null parameter to any of the methods in a BitSet will result in a NullPointerException.

Upvotes: 17

jli
jli

Reputation: 6623

You can store them as byte arrays, then access the bits individually. Then to XOR them you can merely XOR the bytes (it is a bitwise operation).

Of course it doesn't have to be a byte array (could be an array of int types or whatever you want), since everything is stored in binary in the end.

Upvotes: 0

Related Questions