Working With Binary Numbers

J

Jubz

I am writing an algorith that computes a CRC based on a polynomial
function. In its simplest form, the method receives a String of the bit
stream as the first parameter, and another String representing the
generator bits.
At some point, I need the divide the data bit stream as binary by the
generator bits and retain the remainder for use in the CRC computation.
So, what I'm looking for is a way to represent binary numbers in Java.
Is there a simple way (like how we represent hex using 0x prefix), and
what kinds of operators can be executed on these binary number? I'm
hoping a division or modulo is at least possible.
Thanks.
 
O

Oliver Wong

Jubz said:
I am writing an algorith that computes a CRC based on a polynomial
function. In its simplest form, the method receives a String of the bit
stream as the first parameter, and another String representing the
generator bits.
At some point, I need the divide the data bit stream as binary by the
generator bits and retain the remainder for use in the CRC computation.
So, what I'm looking for is a way to represent binary numbers in Java.
Is there a simple way (like how we represent hex using 0x prefix), and
what kinds of operators can be executed on these binary number? I'm
hoping a division or modulo is at least possible.
Thanks.

You need to realize there's a difference between the number itself, and
it's representation. For example "0xC", "12", "014" and "b1100" are four
different representations, but they all refer to the same number.

Therefore, you can use the "int" primitive type in Java to represent
binary numbers, and all the operators valid on int are available to you
(including division and modulo).

As for converting a String containing the binary representation of a
number to an int, take a look at Integer.parseInt().

- Oliver
 
J

Jubz

Alright, so supposing I get the String "110010000" for the data stream,
and "1101" from the generator. In Java code, how do I create int
variables with these values as binary? I tried something like:
int data = b110010000;
int gen = b1101;
and neither seems to work. I guess what I need is a means to read a
String as given into binary, and process the results as a binary number
directly.
 
J

Jubz

Its good you understand these things very well/quickly, unlike me. I do
know what he meant about representations. My question is: without
converting to any other formats (for purposes of visual
representation), can Java take a String, assume the String is binary as
given, and work on it as such?
Is there a prefix for binary numbers that I can append ahead of the
given String so that it now is known as a binary number, much like hex
has 0x?
 
O

Oliver Wong

Jubz said:
Alright, so supposing I get the String "110010000" for the data stream,
and "1101" from the generator. In Java code, how do I create int
variables with these values as binary? I tried something like:
int data = b110010000;
int gen = b1101;
and neither seems to work. I guess what I need is a means to read a
String as given into binary, and process the results as a binary number
directly.

The Integer.parseInt() method I mentioned is documented here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)

So you want something like

int data = Integer.parseInt("110010000", 2);

- Oliver
 
J

Jubz

Thank you, Oliver. The following worked (as a quick test):
String dataInput = "110010000";
String genInput = "1101";

int data = Integer.parseInt(dataInput, 2);
int gen = Integer.parseInt(genInput, 2);

int crc = data % gen;
String crcOutput = Integer.toBinaryString(crc);
System.out.println(crcOutput);
So anytime I need to visually display any data anywhere, the
String.toBinaryString() method works perfectly, with simplicity. I
supposed binary numbers have no prefix per se ... you'd need to convert
them to another representation and work that way.

Something I was interested in was the ability to do shifting and such
on the binary numbers. Is anyone familiar with the BitSet classes and
how they implement this functionality?
 
G

Gordon Beaton

Alright, so supposing I get the String "110010000" for the data
stream, and "1101" from the generator. In Java code, how do I create
int variables with these values as binary?

Obviously you didn't even read the post you just replied to, because
it answered your question quite clearly.

/gordon
 
O

Oliver Wong

Jubz said:
My question is: without
converting to any other formats (for purposes of visual
representation), can Java take a String, assume the String is binary as
given, and work on it as such?

It's not the Java language itself which is doing the conversion from
Strings to numbers. Rather, it's the code inside Integer.parseInt(). I
believe if you wanted to, you could implement your own version of
parseInt(). It would probably look something like:

<roughPseudoCode>
for each character in the String {
multiply the character interpreted as a digit by the radix raised to the
power of its index (e.g. is the index is 5, the radix is 2, and the
character is '1', you have 1 * (2 ^ 5) == 32), and add this result to the
current running total.
}
Is there a prefix for binary numbers that I can append ahead of the
given String so that it now is known as a binary number, much like hex
has 0x?

Technically, the stuff after the 0x is not considered a String by the
Java compiler. The compiler only recognizes string literals by seeing the "
character. When it sees the sequence 0x, it knows that what's coming up is
an integer written in hexadecimal, and the compiler will treat this as an
integer the whole time, with no conversion to or from String.

You can read more about how the Java compiler treats integer literals at
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1

It talks about decimal, hexadecimal, and octal integer literals, but no
binary integer literals, so I guess it's not supported. When I wrote
"b1101011", I was just making up my own notation for that.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top