Home > Enterprise >  Creating and analyzing a control word in C Code
Creating and analyzing a control word in C Code

Time:08-26

I wanted to create and analyze a 16-bit control word, this should be done in C code. Each bit should be evaluated, since each bit has a specific function. There can be an instruction that several bits should come and be analyzed at the same time. Does anyone have a solution for such a problem. So far I can check which bit is set, but I don't like the solution because I have 16 IF branches. Can this be simplified? Assuming the value 2049 comes, bit 0 and bit 11 are set. It would be good if there was a simple function for that. Each bit has a function whether HIGH or LOW.(at the end it should run on a microcontroller. I only used printf for testing)

Thank you for your help.

int main()
{
    uint16_t NUM; //to store number
    printf("Enter an 16 bits integer number: ");
    scanf("%d",&NUM);


    //checking bit status
    if(NUM & (1<<0))
    {
        printf("Bit number %d is SET in number %d.\n",0,NUM);
    }
    if(NUM & (1<<1))
    {
        printf("Bit number %d is SET in number %d.\n",1,NUM);
    }
    if(NUM & (1<<2))
    {
        printf("Bit number %d is SET in number %d.\n",2,NUM);
    }
    if(NUM & (1<<3))
    {
        printf("Bit number %d is SET in number %d.\n",3,NUM);
    }
    if(NUM & (1<<4))
    {
        printf("Bit number %d is SET in number %d.\n",4,NUM);
    }
    if(NUM & (1<<5))
    {
        printf("Bit number %d is SET in number %d.\n",5,NUM);
    }
    if(NUM & (1<<6))
    {
        printf("Bit number %d is SET in number %d.\n",6,NUM);
    }
    if(NUM & (1<<7))
    {
        printf("Bit number %d is SET in number %d.\n",7,NUM);
    }
    if(NUM & (1<<8))
    {
        printf("Bit number %d is SET in number %d.\n",8,NUM);
    }
    if(NUM & (1<<9))
    {
        printf("Bit number %d is SET in number %d.\n",9,NUM);
    }
    if(NUM & (1<<10))
    {
        printf("Bit number %d is SET in number %d.\n",10,NUM);
    }
    if(NUM & (1<<11))
    {
        printf("Bit number %d is SET in number %d.\n",11,NUM);
    }
    if(NUM & (1<<12))
    {
        printf("Bit number %d is SET in number %d.\n",12,NUM);
    }
    if(NUM & (1<<13))
    {
        printf("Bit number %d is SET in number %d.\n",13,NUM);
    }
    if(NUM & (1<<14))
    {
        printf("Bit number %d is SET in number %d.\n",14,NUM);
    }
    if(NUM & (1<<15))
    {
        printf("Bit number %d is SET in number %d.\n",15,NUM);
    }
    if(NUM & (1<<16))
    {
        printf("Bit number %d is SET in number %d.\n",16,NUM);
    }
    return 0;
}

CodePudding user response:

You are going to have to learn a lot about boolean operations if you want to move forward. There's lots of resources on the web.

The following may give you an idea of what you might need to think about. It is presented as-is.

/* Only a sampling of all 16 */
#define BIT__0 0x0001
#define BIT__1 0x0002
#define BIT__2 0x0004
#define BIT__8 0x0100
#define BIT__9 0x0200
#define BIT_10 0x0400
#define BIT_11 0x0800

int main() {

    printf( "Combining bits 0 and 11 = %d decimal %X hex\n",
        BIT__0 | BIT_11, BIT__0 | BIT_11 );

    uint16_t reading = 0x102;

    uint16_t trigger = BIT__8 | BIT__1;

    printf( "reading(%X) %s\n",
        reading, (reading ^ trigger) == 0 ? "Matched" : "No match" );

    reading |= 0x01;

    printf( "reading(%X) %s\n",
        reading, (reading ^ trigger) == 0 ? "Matched" : "No match" );

    return 0;
}

Output:

Combining bits 0 and 11 = 2049 decimal 801 hex
reading(102) Matched
reading(103) No match

CodePudding user response:

this is my current solution. is there a possibility to make this into a lookup table so that the queries are faster? LOW and HIGH always have separate functions.

#include <stdio.h>
#include <stdint.h>

uint16_t bitCheck(uint16_t Controlword, int16_t BIT);

int main(void){

   uint16_t Controlword = 0b1110100100010011;


   printf("Controlword = %d\n", Controlword);

   if ( bitCheck( Controlword, 0) == 1 ){
      printf("BIT %d HIGH\n", 0);
   }else{
      printf("BIT %d LOW\n", 0);
   }

    if ( bitCheck( Controlword, 1) == 1 ){
      printf("BIT %d HIGH\n", 1);
   }else{
      printf("BIT %d LOW\n", 1);
   }

   if ( bitCheck( Controlword, 2) == 1 ){
      printf("BIT %d HIGH\n", 2);
   }else{
      printf("BIT %d LOW\n", 2);
   }

   if ( bitCheck( Controlword, 3) == 1 ){
      printf("BIT %d HIGH\n", 3);
   }else{
      printf("BIT %d LOW\n", 3);
   }

   if ( bitCheck( Controlword, 4) == 1 ){
      printf("BIT %d HIGH\n", 4);
   }else{
      printf("BIT %d LOW\n", 4);
   }

    if ( bitCheck( Controlword, 5) == 1 ){
      printf("BIT %d HIGH\n", 5);
   }else{
      printf("BIT %d LOW\n", 5);
   }

   if ( bitCheck( Controlword, 6) == 1 ){
      printf("BIT %d HIGH\n", 6);
   }else{
      printf("BIT %d LOW\n", 6);
   }

   if ( bitCheck( Controlword, 7) == 1 ){
    printf("BIT %d HIGH\n", 7);
   }else{
    printf("BIT %d LOW\n", 7);
   }

    if ( bitCheck( Controlword, 8) == 1 ){
     printf("BIT %d HIGH\n", 8);
   }else{
      printf("BIT %d LOW\n", 8);
   }

   if ( bitCheck( Controlword, 9) == 1 ){
    printf("BIT %d HIGH\n", 9);
   }else{
      printf("BIT %d LOW\n", 9);
   }

    if ( bitCheck( Controlword, 10) == 1 ){
    printf("BIT %d HIGH\n", 10);
   }else{
      printf("BIT %d LOW\n", 10);
   }

    if ( bitCheck( Controlword, 11) == 1 ){
    printf("BIT %d HIGH\n", 11);
   }else{
      printf("BIT %d LOW\n", 11);
   }

    if ( bitCheck( Controlword, 12) == 1 ){
    printf("BIT %d HIGH\n", 12);
   }else{
      printf("BIT %d LOW\n", 12);
   }

    if ( bitCheck( Controlword, 13) == 1 ){
    printf("BIT %d HIGH\n", 13);
   }else{
      printf("BIT %d LOW\n", 13);
   }

    if ( bitCheck( Controlword, 14) == 1 ){
    printf("BIT %d HIGH\n", 14);
   }else{
      printf("BIT %d LOW\n", 14);
   }

    if ( bitCheck( Controlword, 15) == 1 ){
    printf("BIT %d HIGH", 15);
   }else{
      printf("BIT %d LOW\n", 15);
   }

}


uint16_t bitCheck(uint16_t controlword, int16_t BIT){
   if ( (controlword >> BIT ) & 1){
      return 1;
   }else{
      return 0;
   }
}
  • Related