# Re: seeking bitwise operations solution

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

1. ### E. Robert TisdaleGuest

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

2. ### AlexGuest

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

3. ### Martin DickoppGuest

(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
4. ### Nick AustinGuest

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
5. ### AlexGuest

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

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