[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looking for Long Tooth...





On Wed, 9 Apr 2003, Scott P. Smith wrote:

> I am a little surprised by this question and the other one like it a few
> weeks ago.  I have a sneaky feeling that I am helping a college student do
> his homework, but I'll respond anyway.

I don't find it as surprising. Most of the bitwise manipulation I've come
into direct contact with has not been very necessary, but just a result of
old C habits being transferred to the language in question. Irritating
things like:

 i ^= i;   or somesuch.

Many Java developers out there have not spent time working in C, and in
many other languages there's not the need for the low-level bitwise stuff.

> There are infinitely many ways these operators can be useful.. But I'll just
> give a few examples:

Major ones I've come into contact with are encryption algorithms and
encoding algorithms. At work we encode a series of numbers into one long
for transmission. I've found BigInteger to have surprising powers here at
extracting the values. [for when our long got larger than a java long]

Having a look at the source to an encryption algorithm will see some very
important uses of bit-shifting.

> * Data Compression - Many data compression algorithms compress byte streams
> into bit streams.  To create (and later decode) the bit streams, you need
> bitwise operators.
>
> * Non-Java Binary File I/O - When working with binary files that were
> created in C, C++, etc. you will see a lot of use of things like unsigned
> 16, and 32 bit values. Since Java doesn't support these, you have to use
> bitwise operators to put the uint16 into a Java int32. And vise versa.

Apache's POI project is probably a good example of this. It reads MS
Office formats and as far as I know is full of a need to bit-shift. Having
a look at their source is probably an education.

> * Arbitrarily Precision Integer Math - Math packages that implement 64, 128,
> 256 bit integers, etc.  These packages use bitwise operators (at least I
> think they do).

BigInteger :)


Hen