Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: seeking bitwise operations solution (http://www.velocityreviews.com/forums/t314800-re-seeking-bitwise-operations-solution.html)

 E. Robert Tisdale 08-17-2003 06:20 AM

Re: seeking bitwise operations solution

Nick Austin wrote:
> On 16 Aug 2003 22:30:10 -0700, alexr@bellnet.ca (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 08-17-2003 09:21 PM

Re: seeking bitwise operations solution

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.

Alex

"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message news:<3F3F1EA5.1090701@jpl.nasa.gov>...
> Nick Austin wrote:
> > On 16 Aug 2003 22:30:10 -0700, alexr@bellnet.ca (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);

 Martin Dickopp 08-17-2003 09:57 PM

Re: seeking bitwise operations solution

alexr@bellnet.ca (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

 Nick Austin 08-17-2003 10:13 PM

Re: seeking bitwise operations solution

On 17 Aug 2003 14:21:58 -0700, alexr@bellnet.ca (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 08-18-2003 02:15 AM

Re: seeking bitwise operations solution

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 <nickDIGITONE@nildram.co.uk> wrote in message news:<sfuvjvkju384lfq6dqf6e16035c7hd78fq@4ax.com>. ..
> On 17 Aug 2003 14:21:58 -0700, alexr@bellnet.ca (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.

 All times are GMT. The time now is 12:17 AM.