Re: seeking bitwise operations solution

Discussion in 'C Programming' started by E. Robert Tisdale, Aug 17, 2003.

  1. Nick Austin wrote:
    > On 16 Aug 2003 22:30:10 -0700, (Alex) wrote:
    >
    >
    >>Hi
    >>
    >>Is there any way to arrive at the following results, using only
    >>bitwise operators:
    >>
    >>in out
    >>=== ===
    >>0 0 0 0
    >>0 1 1 1
    >>1 0 1 1
    >>1 1 1 1

    >
    >
    > What does this table represent? If you mean:
    >
    > in out
    > ======= =======
    > a=0 b=0 a=0 b=0
    > a=0 b=1 a=1 b=1
    > a=1 b=0 a=1 b=1
    > a=1 b=1 a=1 b=1
    >
    > Where a and b are unsigned int then:
    >
    > a = b = a | b;


    Or, if a and b are the two least significant bits
    of an unsigned int in

    unsigned int out = in&1 | (in >> 1)&1;
    out |= (out << 1);
     
    E. Robert Tisdale, Aug 17, 2003
    #1
    1. Advertising

  2. E. Robert Tisdale

    Alex Guest

    Thanks Nick, for pointing me in the right track.

    Alex

    LPS: I have 2-bit depth image data that I would like to mask. Data is
    contained in a sequence of 6200 bytes, where each byte contains the
    color table index values of 4 pixels (2 bits per pixel):

    0 0 = table index 0 (currently associated color = white)
    0 1 = table index 1 (currently associated color = light gray)
    1 0 = table index 2 (currently associated color = dark gray)
    1 1 = table index 3 (currently associated color = black)

    For example, a sequence of (white, black, dark grey, light gray)
    would be stored as 0 0 1 1 1 0 0 1.

    What I want to do is convert that sequence such that
    all non-white pixels will become black.

    For example, the sequence 0 0 1 1 1 0 0 1 would become
    0 0 1 1 1 1 1 1.

    And I want to do that as fast as possible.

    Thanks for your help.
    Alex





    "E. Robert Tisdale" <> wrote in message news:<>...
    > Nick Austin wrote:
    > > On 16 Aug 2003 22:30:10 -0700, (Alex) wrote:
    > >
    > >
    > >>Hi
    > >>
    > >>Is there any way to arrive at the following results, using only
    > >>bitwise operators:
    > >>
    > >>in out
    > >>=== ===
    > >>0 0 0 0
    > >>0 1 1 1
    > >>1 0 1 1
    > >>1 1 1 1

    > >
    > >
    > > What does this table represent? If you mean:
    > >
    > > in out
    > > ======= =======
    > > a=0 b=0 a=0 b=0
    > > a=0 b=1 a=1 b=1
    > > a=1 b=0 a=1 b=1
    > > a=1 b=1 a=1 b=1
    > >
    > > Where a and b are unsigned int then:
    > >
    > > a = b = a | b;

    >
    > Or, if a and b are the two least significant bits
    > of an unsigned int in
    >
    > unsigned int out = in&1 | (in >> 1)&1;
    > out |= (out << 1);
     
    Alex, Aug 17, 2003
    #2
    1. Advertising

  3. (Alex) writes:

    > LPS: I have 2-bit depth image data that I would like to mask. Data is
    > contained in a sequence of 6200 bytes, where each byte contains the
    > color table index values of 4 pixels (2 bits per pixel):
    >
    > 0 0 = table index 0 (currently associated color = white)
    > 0 1 = table index 1 (currently associated color = light gray)
    > 1 0 = table index 2 (currently associated color = dark gray)
    > 1 1 = table index 3 (currently associated color = black)
    >
    > For example, a sequence of (white, black, dark grey, light gray)
    > would be stored as 0 0 1 1 1 0 0 1.
    >
    > What I want to do is convert that sequence such that
    > all non-white pixels will become black.
    >
    > For example, the sequence 0 0 1 1 1 0 0 1 would become
    > 0 0 1 1 1 1 1 1.
    >
    > And I want to do that as fast as possible.


    You might want to use a look-up table which maps each possible 8-bit
    sequence to the required output value. Such a table would have 256 entries,
    so it'd be small enough for most applications/environments.

    Martin
     
    Martin Dickopp, Aug 17, 2003
    #3
  4. E. Robert Tisdale

    Nick Austin Guest

    On 17 Aug 2003 14:21:58 -0700, (Alex) wrote:

    >Thanks Nick, for pointing me in the right track.
    >
    >Alex
    >
    >LPS: I have 2-bit depth image data that I would like to mask. Data is
    >contained in a sequence of 6200 bytes, where each byte contains the
    >color table index values of 4 pixels (2 bits per pixel):
    >
    >0 0 = table index 0 (currently associated color = white)
    >0 1 = table index 1 (currently associated color = light gray)
    >1 0 = table index 2 (currently associated color = dark gray)
    >1 1 = table index 3 (currently associated color = black)
    >
    >For example, a sequence of (white, black, dark grey, light gray)
    >would be stored as 0 0 1 1 1 0 0 1.
    >
    >What I want to do is convert that sequence such that
    >all non-white pixels will become black.
    >
    >For example, the sequence 0 0 1 1 1 0 0 1 would become
    >0 0 1 1 1 1 1 1.


    Well that's easy. Convert all four pixels as one operation:

    newcolor = ( (oldcolor<<1) & 0xAA ) | oldcolor |
    ( (oldcolor>>1) & 0x55 );

    >And I want to do that as fast as possible.


    So make it a table look-up:

    newcolor = conversion[oldcolor];

    Of course you need to populate the table first. Create a function
    that does this for all 256 values and call that function during
    initialisation.

    Alternatively make the populate function into a separate program
    and paste all 256 result values into your program as an initialiser:

    const unsigned char conversion[] =
    {
    0x00, 0x03, 0x03, 0x03, 0x0c, 0x0f, 0x0f, 0x0f,
    0x0c, 0x0f, 0x0f, 0x0f, 0x0c, 0x0f, 0x0f, 0x0f,
    0x30, 0x33, 0x33, 0x33, 0x3c, 0x3f, 0x3f, 0x3f,
    0x3c, 0x3f, 0x3f, 0x3f, 0x3c, 0x3f, 0x3f, 0x3f,
    /* etc... */
    }

    Nick.
     
    Nick Austin, Aug 17, 2003
    #4
  5. E. Robert Tisdale

    Alex Guest

    Thanks to everyone for their help. Problem solved thanks to you.
    Extra side effect: a lesson in modesty was obtained.

    Coming soon: more easy questions !

    :)
    Alex


    Nick Austin <> wrote in message news:<>...
    > On 17 Aug 2003 14:21:58 -0700, (Alex) wrote:
    >
    > >Thanks Nick, for pointing me in the right track.
    > >
    > >Alex
    > >
    > >LPS: I have 2-bit depth image data that I would like to mask. Data is
    > >contained in a sequence of 6200 bytes, where each byte contains the
    > >color table index values of 4 pixels (2 bits per pixel):
    > >
    > >0 0 = table index 0 (currently associated color = white)
    > >0 1 = table index 1 (currently associated color = light gray)
    > >1 0 = table index 2 (currently associated color = dark gray)
    > >1 1 = table index 3 (currently associated color = black)
    > >
    > >For example, a sequence of (white, black, dark grey, light gray)
    > >would be stored as 0 0 1 1 1 0 0 1.
    > >
    > >What I want to do is convert that sequence such that
    > >all non-white pixels will become black.
    > >
    > >For example, the sequence 0 0 1 1 1 0 0 1 would become
    > >0 0 1 1 1 1 1 1.

    >
    > Well that's easy. Convert all four pixels as one operation:
    >
    > newcolor = ( (oldcolor<<1) & 0xAA ) | oldcolor |
    > ( (oldcolor>>1) & 0x55 );
    >
    > >And I want to do that as fast as possible.

    >
    > So make it a table look-up:
    >
    > newcolor = conversion[oldcolor];
    >
    > Of course you need to populate the table first. Create a function
    > that does this for all 256 values and call that function during
    > initialisation.
    >
    > Alternatively make the populate function into a separate program
    > and paste all 256 result values into your program as an initialiser:
    >
    > const unsigned char conversion[] =
    > {
    > 0x00, 0x03, 0x03, 0x03, 0x0c, 0x0f, 0x0f, 0x0f,
    > 0x0c, 0x0f, 0x0f, 0x0f, 0x0c, 0x0f, 0x0f, 0x0f,
    > 0x30, 0x33, 0x33, 0x33, 0x3c, 0x3f, 0x3f, 0x3f,
    > 0x3c, 0x3f, 0x3f, 0x3f, 0x3c, 0x3f, 0x3f, 0x3f,
    > /* etc... */
    > }
    >
    > Nick.
     
    Alex, Aug 18, 2003
    #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. =?Utf-8?B?Sm9u?=

    BitWise Operations

    =?Utf-8?B?Sm9u?=, Jan 23, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,377
    =?Utf-8?B?Sm9u?=
    Jan 24, 2006
  2. Carl
    Replies:
    3
    Views:
    4,102
    John Machin
    Aug 19, 2005
  3. Chris
    Replies:
    6
    Views:
    2,413
    Daniel Pitts
    Jan 5, 2007
  4. Alan Holloway

    newbie seeks insight on bitwise operations..

    Alan Holloway, Jul 28, 2004, in forum: C Programming
    Replies:
    11
    Views:
    439
    Keith Thompson
    Jul 30, 2004
  5. Replies:
    6
    Views:
    589
    Victor Bazarov
    Apr 19, 2006
Loading...

Share This Page