Bit Masking Question

Blair P. Houghton bhoughto at pima.intel.com
Mon Jun 24 16:26:51 AEST 1991


In article <12191 at hub.ucsb.edu> angst at cs.ucsb.edu (Hopelessly in love w/Donna Reed) writes:

Yeah, she's a babe, alright.  A member of a dying breed.
Unless you count that such competence has moved into the
cubicles next to me with rather heartening frequency.

>What I'm doing is reading characters from a file and I'd like to 
>categorise the characters into 3 groups based on their 2 most-
>significant bits.  Group 1 has '11' in their MSb's, group 2 has
>'10' in their MSb's, and group 3 has '0X' (X = don't care) in
>their MSb's.

The only other (sensible, non-shifting) way to do it is

	if ( c & 0x80 )
	    if ( c & 0x40 )
		/* ops for Group 1 */
	    else
		/* ops for Group 2 */
	else
	    /* ops for Group 3 */

But this uses more than one `&' operation, which may as much
as double the decision time (although the optimizer may just
surprise you).

I prefer your way.  However, you seem to believe that the
two significant bits can be something other than `11',
`10', `01', and `00' (note the impossible-to-reach
printf()).  That is, after the cases for FMASK and REPMASK,
you should have only the default as the case for Group 3
(although the optimizer may just surprise me).

				--Blair
				  "*i = i[0];"



More information about the Comp.lang.c mailing list