Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Mask multiple bytes at once

Reply
Thread Tools

Mask multiple bytes at once

 
 
lancer6238@yahoo.com
Guest
Posts: n/a
 
      01-18-2010
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
 
Reply With Quote
 
 
 
 
Andrew Poelstra
Guest
Posts: n/a
 
      01-18-2010
On 2010-01-18, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-18-2010
Andrew Poelstra wrote:
> On 2010-01-18, (E-Mail Removed) <(E-Mail Removed)> wrote:
>> 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?

>
> 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.

--
Ian Collins
 
Reply With Quote
 
gil_johnson
Guest
Posts: n/a
 
      01-19-2010
On Jan 17, 9:55*pm, "(E-Mail Removed)" <(E-Mail Removed)>
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
 
Reply With Quote
 
James Harris
Guest
Posts: n/a
 
      01-19-2010
On 18 Jan, 03:55, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion mask in hex to bit mask Marcin Tyman Ruby 4 05-06-2008 08:15 PM
Want to mask all the rows in a column at once? RG007 Java 2 01-17-2006 08:43 AM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
[networking] Convert subnet mask <=> mask length 187 Perl Misc 2 07-29-2004 10:31 AM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM



Advertisments