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

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

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

    Re: Bit mask

    Eric Sosman, Jan 7, 2004, in forum: C Programming
    Replies:
    0
    Views:
    443
    Eric Sosman
    Jan 7, 2004
  3. Eric

    Re: Bit mask

    Eric, Jan 7, 2004, in forum: C Programming
    Replies:
    0
    Views:
    439
  4. Marcin Tyman

    Conversion mask in hex to bit mask

    Marcin Tyman, May 6, 2008, in forum: Ruby
    Replies:
    4
    Views:
    776
    Robert Klemme
    May 6, 2008
  5. 187
    Replies:
    2
    Views:
    539
    Bart Lateur
    Jul 29, 2004
Loading...

Share This Page