Re: Bit mask

Discussion in 'C Programming' started by Mark A. Odell, Jan 7, 2004.

  1. Joona I Palaste <> wrote in
    news:bthisv$gps$:

    >> Example:

    >
    >> 01011[010] --> 01011[000]

    >
    >> Thanks!

    >
    > Well, a really simple way would be ANDing with ~7.
    > For example:
    > unsigned char x = 0x5A; /* the same as binary 01011010 */
    > x = x & ~7; /* clear last 3 bits */
    > Is it efficient? It depends on your implementation.


    Why not do:

    unsigned char x = 0x5A;

    x &= (unsigned char) ~0x07;

    --
    - Mark ->
    --
     
    Mark A. Odell, Jan 7, 2004
    #1
    1. Advertisements

  2. Mark A. Odell <> scribbled the following:
    > Joona I Palaste <> wrote in
    > news:bthisv$gps$:
    >>> Example:

    >>
    >>> 01011[010] --> 01011[000]

    >>
    >>> Thanks!

    >>
    >> Well, a really simple way would be ANDing with ~7.
    >> For example:
    >> unsigned char x = 0x5A; /* the same as binary 01011010 */
    >> x = x & ~7; /* clear last 3 bits */
    >> Is it efficient? It depends on your implementation.


    > Why not do:


    > unsigned char x = 0x5A;


    > x &= (unsigned char) ~0x07;


    It's practically the same thing, and whatever efficiency difference
    there would be will probably get lost in compiler optimisation. I just
    thought my version was clearer to read. Suit yourself.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To know me IS to love me."
    - JIPsoft
     
    Joona I Palaste, Jan 7, 2004
    #2
    1. Advertisements

  3. Joona I Palaste <> wrote in
    news:bthjfr$h15$:


    >>> Well, a really simple way would be ANDing with ~7.
    >>> For example:
    >>> unsigned char x = 0x5A; /* the same as binary 01011010 */
    >>> x = x & ~7; /* clear last 3 bits */
    >>> Is it efficient? It depends on your implementation.

    >
    >> Why not do:

    >
    >> unsigned char x = 0x5A;

    >
    >> x &= (unsigned char) ~0x07;

    >
    > It's practically the same thing, and whatever efficiency difference
    > there would be will probably get lost in compiler optimisation. I just
    > thought my version was clearer to read. Suit yourself.


    I meant nothing WRT efficiency, just that I thought that &= is the more
    common idiom and that you needed to cast ~0x07 to unsigned char.

    --
    - Mark ->
    --
     
    Mark A. Odell, Jan 7, 2004
    #3
    1. Advertisements

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. ex laguna
    Replies:
    11
    Views:
    1,579
    Peter Nilsson
    Jul 27, 2003
  2. Als

    Bit mask

    Als, Jan 7, 2004, in forum: C Programming
    Replies:
    24
    Views:
    1,658
  3. Replies:
    3
    Views:
    2,259
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    1,378
    Juha Nieminen
    Aug 22, 2007
  5. Francois Grieu

    Improving mask-to-bit macro

    Francois Grieu, Oct 13, 2009, in forum: C Programming
    Replies:
    12
    Views:
    1,148
    Tim Rentsch
    Oct 14, 2009
  6. Marcin Tyman

    Conversion mask in hex to bit mask

    Marcin Tyman, May 6, 2008, in forum: Ruby
    Replies:
    4
    Views:
    1,201
    Robert Klemme
    May 6, 2008
  7. 187
    Replies:
    2
    Views:
    854
    Bart Lateur
    Jul 29, 2004
  8. Jeff.M
    Replies:
    6
    Views:
    398
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...