Mask multiple bytes at once

Discussion in 'C Programming' started by lancer6238@yahoo.com, Jan 18, 2010.

  1. Guest

    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
    , Jan 18, 2010
    #1
    1. Advertising

  2. On 2010-01-18, <> 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.
    Andrew Poelstra, Jan 18, 2010
    #2
    1. Advertising

  3. Ian Collins Guest

    Andrew Poelstra wrote:
    > On 2010-01-18, <> 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
    Ian Collins, Jan 18, 2010
    #3
  4. gil_johnson Guest

    On Jan 17, 9:55 pm, "" <>
    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
    gil_johnson, Jan 19, 2010
    #4
  5. James Harris Guest

    On 18 Jan, 03:55, "" <> 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
    James Harris, Jan 19, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    6,015
    Jason Collins
    Feb 18, 2004
  2. RG007
    Replies:
    2
    Views:
    346
    Venky
    Jan 17, 2006
  3. Marcin Tyman

    Conversion mask in hex to bit mask

    Marcin Tyman, May 6, 2008, in forum: Ruby
    Replies:
    4
    Views:
    809
    Robert Klemme
    May 6, 2008
  4. 187
    Replies:
    2
    Views:
    556
    Bart Lateur
    Jul 29, 2004
  5. Gancy
    Replies:
    4
    Views:
    176
    Rasto Levrinc
    Feb 3, 2005
Loading...

Share This Page