This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

get bit position

is there a way to get the position of a bit in a bit filed struct ?

for example,

if I have

struct {
Uint16 a:1; Uint16 b:1; Uint16 c:1; } myS;

is there a way to get bit position of "a", for example if I have to call a routine like this

setMyBit(&myS, /* bit pos of a */)

  • yes, always assignment start from LSB.
    So, a = BIT0 (1), b = BIT1 (2), C = BIT2 (4)

    myS.b = 1 will set BIT1.

    See below a sample
    //-----------------------------------------------------------------------
    struct SYSTEM_BITS { // bits description
    Uint16 POWERINGUP :1; // 0
    Uint16 VBATLOW :1; // 1
    Uint16 VBATHIGH :1; // 2
    Uint16 CON2DISABL :1; // 3
    Uint16 CON1DISABL :1; // 4
    Uint16 CHOPPERDISABL :1; // 5
    Uint16 INVRTRDISABL :1; // 6
    Uint16 SOFTSTART :1; // 7
    Uint16 CURRMODE :1; // 8
    Uint16 OVERLOAD :1; // 9
    Uint16 VCESAT :1; // 10
    Uint16 LATCHFAULT :1; // 11
    Uint16 SHORTCKT :1; // 12
    Uint16 SURGETYPE1:1; // 13
    Uint16 SURGETYPE2:1; // 14
    Uint16 BITSVALID :1; // 15
    };


    typedef union {
    N16 all;
    struct SYSTEM_BITS bit;
    } SYSMODE;

    //define variable
    SYSMODE SystemFlag;


    //Now addressing individual bits

    SystemFlag.bit.POWERINGUP = 1;
    SystemFlag.bit.LATCHFAULT = 0;