[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Brug af flags i funktioner



Mads Sejersen <sslug@sslug> writes:

> Hejsa.
> 
> Jeg vil gerne lave en funktion med forskellige flags i.
> Noget i retning af
> 
> enum flags{
> 	FLAG_1 = 1 << 1,
> 	FLAG_2 = 1 << 2,
> 	FLAG_3 = 1 << 3
> };
> 
> void f(flags f){
> 	...
> }
> 
> Man skal så kunne kalde funktionen med f(FLAG_1 | FLAG_2);
> 
> Men hvordan er det så lige at jeg herefter finder ud af hvilke flags
> der er sat.
> Jeg læste det engang et eller andet sted, men har glemt hvor.

Du bruger bitvis "and", (ampersand, &) til at teste hver enkelt flag.
F.eks.
void f(flags f){

if(f & FLAG_1)
        printf("FLAG_1 er sat\n");

if(f & FLAG_2)
        printf("FLAG_2 er sat\n");

/* etc. */


}

-- 
Christian Hemmingsen


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *