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 / January 2007

Tip: Looking for answers? Try searching our database.

Bitwise operations on a byte array

Thread view: 
Chris - 04 Jan 2007 22:46 GMT
I'd like to do something like this:

byte [] array0 = ...
byte [] array1 = ...

array0 |= array1;

The idea being that all the bytes in array0 get ORd with the ones in
array1. Is there an efficient way to do this?

This is just too ugly and unnecessarily slow:

for (int i = 0; i < array0.length; i++) {
    array0[i] |= array1[i];
}
Daniel Pitts - 05 Jan 2007 01:20 GMT
> I'd like to do something like this:
>
[quoted text clipped - 11 lines]
>      array0[i] |= array1[i];
> }

Have you considered using a BitSet instead?
Chris - 05 Jan 2007 02:05 GMT
>> I'd like to do something like this:
>>
[quoted text clipped - 13 lines]
>
> Have you considered using a BitSet instead?

Internally, a BitSet is just an array of ints or longs, and faces the
same problem.
Ehsan Khoddam mohammadi - 05 Jan 2007 02:52 GMT
I think all bitwise operators in java cast vars to integrals like int
and long, so it's prefered to use int and long only
> >> I'd like to do something like this:
> >>
[quoted text clipped - 16 lines]
> Internally, a BitSet is just an array of ints or longs, and faces the
> same problem.
Patricia Shanahan - 05 Jan 2007 05:30 GMT
>>> I'd like to do something like this:
>>>
[quoted text clipped - 16 lines]
> Internally, a BitSet is just an array of ints or longs, and faces the
> same problem.

I'm not sure what you mean by "the same problem". The byte-based method
may be unnecessarily slow, because of the numeric promotions. That does
not apply to BitSet.

The logic for doing a bitwise or in the processor has a finite width,
probably 64 bits. Something, somewhere, must chop the data into 64 bit
chunks and issue a series of commands, one for each chunk. What's so bad
about BitSet doing it?

Patricia
Daniel Pitts - 05 Jan 2007 17:51 GMT
> >> I'd like to do something like this:
> >>
[quoted text clipped - 16 lines]
> Internally, a BitSet is just an array of ints or longs, and faces the
> same problem.
Not exactly the same problem...
BitSet's have the operations defined already, so you don't have a "too
ugly" implementation.
Also, they use integers, for a speed increase of a factor around 4
(could be more).
On most modern processors (as someone else pointed out), you would have
to implement it as a loop through every element anyway. There is no
instruction that I know of that says "For every bit in the range a
through b, or it with the corrisponding bit in the range c through
(c+b-a)"

BitSet is definitly the way to go if you find yourself doing those
operations often.
Jim Korman - 05 Jan 2007 02:04 GMT
>I'd like to do something like this:
>
[quoted text clipped - 11 lines]
>     array0[i] |= array1[i];
>}

1. No way to write |= on arrays, only elements

2. What's ugly about a for loop? If so, hide it in a method.

3. Slow!? For two 10000000 element arrays, my machine
takes just under 50 milliseconds!

Jim


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.