RicardasSim icon

enum flags, while

RicardasSim | PRO | 02/27/20 09:18:43 PM UTC | 0 ⭐ | 695 👁️ | Never ⏰ | []
C |

1.12 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
enum flags {
 
    FLAG_1   = 0x00000001,
    FLAG_2   = 0x00000002,
    FLAG_3   = 0x00000004,
    FLAG_4   = 0x00000008,
    FLAG_5   = 0x00000010,
    FLAG_B_MAX  = 0x7FFFFFFF
 
};
 
int main()
{
 
    uint32_t test_flags = FLAG_2 | FLAG_4;
 
    uint32_t val = test_flags;
 
    uint32_t pos = 0x1;
 
    while( val )
    {
 
        if( val & 1 )
        {
            switch(pos)
            {
                case FLAG_1:
                    printf("FLAG_1 ");
                    break;
 
                case FLAG_2:
                    printf("FLAG_2 ");
                    break;
 
                case FLAG_3:
                    printf("FLAG_3 ");
                    break;
 
                case FLAG_4:
                    printf("FLAG_4 ");
                    break;
 
                case FLAG_5:
                    printf("FLAG_5 ");
                    break;
 
                default:
                    printf("Unhandled ");
                    break;
            }
        }
 
        pos<<=1;
        val>>=1;
    }
 
    printf("\n");
 
    return 0;
}
 
/*
output:
 
FLAG_2 FLAG_4
 
*/

Comments