Mask multiple bytes at once

L

lancer6238

Hi,

I would like to know if it is possible to mask multiple bytes at once.
For example, if I have a char array of 5 elements, and I have a mask
0xFF01F7 that I would like to apply on the first 3 elements of the
array. Is it possible to do this without comparing byte by byte, which
I thought was quite inefficient?

Thank you.

Regards,
Rayne
 
A

Andrew Poelstra

Hi,

I would like to know if it is possible to mask multiple bytes at once.
For example, if I have a char array of 5 elements, and I have a mask
0xFF01F7 that I would like to apply on the first 3 elements of the
array. Is it possible to do this without comparing byte by byte, which
I thought was quite inefficient?

Thank you.

Regards,
Rayne

You can load all the bytes into an unsigned int or long (or in C99,
uint32_t or uint64_t might be safer), and then mask that.

Though to be portable across systems of different endianness you
will need to load the bytes into the integer yourself using shifts
and adds.
 
I

Ian Collins

Andrew said:
You can load all the bytes into an unsigned int or long (or in C99,
uint32_t or uint64_t might be safer), and then mask that.

Though to be portable across systems of different endianness you
will need to load the bytes into the integer yourself using shifts
and adds.

Or simply use an endian appropriate mask, if the mask is fixed or used
more than once.
 
G

gil_johnson

On Jan 17, 9:55 pm, "(e-mail address removed)" <[email protected]>
wrote:
[...]
Is it possible to do this without comparing byte by byte, which
I thought was quite inefficient?
[...]

First, are you sure this is a bottleneck? Are you going to be masking
megabyte size arrays or Is your example realistic, masking 3 bytes
against a small array? Is your program going to spend most of its time
in these comparisons?

If not, it is not a good idea to trade off a tiny increase in speed
for a large loss of readability.

"We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil" - Donald Knuth

Hope this helps,
Gil
 
J

James Harris

Hi,

I would like to know if it is possible to mask multiple bytes at once.
For example, if I have a char array of 5 elements, and I have a mask
0xFF01F7 that I would like to apply on the first 3 elements of the
array. Is it possible to do this without comparing byte by byte, which
I thought was quite inefficient?

First of all, as others have said, this may be a bad optimisation but
that depends on the bigger picture. Don't do it unless it is really
the right thing to do. Keep code simple!

If there's real value in optimising here bear in mind that simple
instructions are usually fast so a few shifts & ands are fairly
harmless. The compiler may even be able to combine them: if on x86 to
something like

mov eax, [array]
and eax, 0xffff01f7

If really necessary and you don't need porability you could write the
asm. Note that I've added 0xff to your mask for the top byte so as not
to change it and that the mask may need to be 0xf701ffff depending on
endianness and the array layout.

James
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top