Coder's Guild Mailing List

Re: Binary Operations

Posted by Weasel on 1999-05-03

Josh Letterman wrote:
> 
> >> >struc flags {
> >> >       unsigned bit7:1;
> >> >       unsigned bit6:1;
> >> >  etc...
> >> >      }
> >> >then just do this:
> >> >flags myflags.bit7=1;
> >>
> >> You know, Dmitry, this *is* a much simpler way. Sometimes, after playing
> >> around with assembler so much, you forget about the niceties of C.
> >
> >But is this method slower than doing it this way?
> >
> >flags = flags | (1 << 7)
> 
> I seriously doubt it. Once compiled, the instruction will probably be the
> same thing.

Actually I think the fastest way was to define some constants:

#define FLAG_ZERO       1
#define FLAG_CARRY      2
#define FLAG_INTERRUPT  4
#define FLAG_DECIMAL    8
....

and then do flags = flags | FLAG_ZERO.

However if you need the bitnumber to stay variable, do it the (1 <<
bitnr) way. btw: this is a direct assembler command (SHL), so it will be
fast! (at least on intels. dunno know about others)

If 
-- 
Weasel                          mailto:palfrader@xxxxxxx.xxx
Peter                   http://www.cosy.sbg.ac.at/~ppalfrad/
------------------------------------------------------------
     We have full klingon console support just in case.
                         -- Alan Cox on linux-kernel