Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Working With Binary Numbers

Thread view: 
Jubz - 11 Apr 2006 20:45 GMT
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.
Oliver Wong - 11 Apr 2006 20:52 GMT
>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
[quoted text clipped - 7 lines]
> 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
Jubz - 11 Apr 2006 21:04 GMT
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.
Gordon Beaton - 11 Apr 2006 21:26 GMT
> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Jubz - 11 Apr 2006 21:28 GMT
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?
Oliver Wong - 11 Apr 2006 22:32 GMT
> 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.
}
</roughPseudoCode>

> 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
Oliver Wong - 11 Apr 2006 21:31 GMT
> 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
[quoted text clipped - 4 lines]
> 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.lan
g.String,%20int
)

   So you want something like

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

   - Oliver
Jubz - 11 Apr 2006 21:44 GMT
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?
Oliver Wong - 11 Apr 2006 22:25 GMT
> 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?

   Bit shifting can be performed on primitive ints using the <<, >> and >>>
operators. See
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19

   - Oliver
Roedy Green - 11 Apr 2006 22:35 GMT
>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.

for source code see http://mindprod.com/jgloss/crc.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.