Re: Bit mask

Discussion in 'C Programming' started by Eric Sosman, Jan 7, 2004.

  1. Eric Sosman

    Eric Sosman Guest

    Als wrote:
    >
    > What's an efficient way to mask a last 3 bits of a 8-bit char and make them
    > all zero?
    >
    > Bit-shifting is possible but not sure if it is efficient enough.
    >
    > Example:
    >
    > 01011[010] --> 01011[000]


    Many or perhaps even most C implementations use an
    eight-bit `char', but that is not actually guaranteed
    by the language, and implementations using wider `char'
    are known to exist. Still:

    unsigned char byte = 0x5A; /* 00...01011010 */
    byte &= ~ 0x07; /* AND with 11...11111000 */
    /* result: 00...01011000 */

    Also, the Standard says nothing about the relative
    efficiency of operations in C. In fact, *you* haven't
    said what you mean by "efficiency!" Do you want minimal
    code size, minimal register usage, minimal execution
    time, minimal debugging time ...?

    --
     
    Eric Sosman, Jan 7, 2004
    #1
    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,404
    Peter Nilsson
    Jul 27, 2003
  2. Mark A. Odell

    Re: Bit mask

    Mark A. Odell, Jan 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    505
    Mark A. Odell
    Jan 7, 2004
  3. Eric

    Re: Bit mask

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

    Conversion mask in hex to bit mask

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

Share This Page