2015年7月21日 星期二

[C] Bitwise operations in C (for-loop to determine value)


/*
 *
 * C Code filename
 * Description:
 * C Code function description
 *
 *
*/


/*! \file C Code filename
    \brief C Code function description
*/

/**************************************************************************/
/*      INCLUDES:                                                         */
/**************************************************************************/
// #include <linux/kernel.h>
#include<stdio.h>

/**************************************************************************/
/*      EXTERNS Declaration:                                              */
/**************************************************************************/

/**************************************************************************/
/*      DEFINES:                                                          */
/**************************************************************************/
typedef enum
{
        ifaceid,
        numberoftriesid,
        timeoutid,
        datablocksizeid,
        DSCPid,
        maxhopcountid,
        END
} TraceRouteParam_ID_e;

/**************************************************************************/
/*      LOCAL DECLARATIONS:                                               */
/**************************************************************************/

// #define Happy (1)

/*************************************************************************/
/*      LOCAL VARIABLES:                                                  */
/**************************************************************************/

// static happy  happy;
const int TraceRouteParam_ID_num = END;

/**************************************************************************/
/*      INTERFACE FUNCTIONS Implementation:                               */
/**************************************************************************/

/**************************************************************************/
/*      LOCAL FUNCTIONS:                                                  */
/**************************************************************************/
/*
#ifdef happy
static int happy (int happy);
#endif
*/

/**************************************************************************/
/*! \fn int happy(unsigned char* happy, struct happy*  happy)
 **************************************************************************
 *  \brief
 *  \brif
 *  \param[in]
 *  \param[in]
 *  \param[out] no output.
 *  \return OK or error status.
 **************************************************************************/
//unsigned int tracerouteparam_ID = 0x3F; /* 0011 1111 */
unsigned int tracerouteparam_ID = 0x00;
// unsigned int tracerouteparam_ID = 0b00111111; /* 0011 1111 */

int OpenSwitch()
{
 tracerouteparam_ID |= 0x01 << ifaceid;
 tracerouteparam_ID |= 0x01 << DSCPid;
}

int main(int argc, char *argv[])
{
    int i = 0;
    OpenSwitch();

    for(i = 0; i < TraceRouteParam_ID_num; i++)
    {
         if ((tracerouteparam_ID >> i) & 0x01)
         {
             switch(i)
             {
                case ifaceid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 1 - XYZ123 \n");
                    }
                    break;
                case numberoftriesid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 2 - XYZ123 \n");
                    }
                    break;
                case timeoutid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 3 - XYZ123 \n");
                    }
                    break;
                case datablocksizeid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 4 - XYZ123 \n");
                    }
                    break;
                case DSCPid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 5 - XYZ123 \n");
                    }
                    break;
                case maxhopcountid:
                    {
                        fprintf(stderr,"GOGO - Debug - Code file - function name 6 - XYZ123 \n");
                    }
                    break;
                default:
                    break;
             }
/*
3e  =  0011 1110
3c  =  0011 1100
38  =  0011 1000
30  =  0011 0000
20  =  0010 0000
0   =  0000 0000
*/
             tracerouteparam_ID &= ~(0x01 << i); /* Clear the bit become 0 */
             fprintf(stderr,"GOGO - Debug - Code file - function name - XYZ123  %x  \n",         tracerouteparam_ID    );
         }
    }

}

Reference:

0 意見:

張貼留言