Bitwise operations on a byte array

C

Chris

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 |= array1;
}
 
D

Daniel Pitts

Chris said:
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 |= array1;
}


Have you considered using a BitSet instead?
 
J

Jim Korman

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 |= array1;
}


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
 
C

Chris

Daniel said:
Chris said:
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 |= array1;
}


Have you considered using a BitSet instead?

Internally, a BitSet is just an array of ints or longs, and faces the
same problem.
 
E

Ehsan Khoddam mohammadi

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
Chris said:
Daniel said:
Chris said:
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 |= array1;
}


Have you considered using a BitSet instead?

Internally, a BitSet is just an array of ints or longs, and faces the
same problem.
 
P

Patricia Shanahan

Chris said:
Daniel said:
Chris said:
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 |= array1;
}


Have you considered using a BitSet instead?

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
 
D

Daniel Pitts

Chris said:
Daniel said:
Chris said:
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 |= array1;
}


Have you considered using a BitSet instead?

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.
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top