-1

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;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Markus
  • 21
  • 1

3 Answers3

2

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
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
1

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;
   }
}
Markus
  • 21
  • 1
  • Sure, one possible solution is to define an array of structures that each have the bit mask or bit index and a pointer to the function to execute. Just learn more C, and you will be able to program that. Unfortunately SO is not the site to be taught in this matter. -- You should take the [tour] to learn what this site is about. Honestly, it will save you from a lot of frustration. – the busybee Aug 25 '22 at 13:01
  • I'm not a professional in c yet, but sorry, could you please send me an approach to this? In terms of function, it works like this, but I always wanted to access the bit directly without making all queries beforehand. Thanks – Markus Aug 25 '22 at 14:12
  • Well, use the terms of my comment as keywords for your web research. Recommendations are off-topic here. – the busybee Aug 25 '22 at 14:19
  • What do you mean by "queries" and why do you think you need to make them "faster"? You're worried about the wrong things. Just write clean code. This code is not clean because it is so repetitive. Use a loop like has been suggested in the comments to your question. You may eventually end up with a struct array like busybee suggests but that is way beyond where you're at now. – kkrambo Aug 25 '22 at 15:13
  • why should a loop help me? if i cant longer order the instructions of the individual bits, in the case of high or low and i know how to use a loop. I'm concerned if a certain bit is set then an instruction is carried out. – Markus Aug 25 '22 at 15:24
0

You might want to create an array, holding each bitmask as the corresponding function calls.

typedef struct {
    uint16_t    check_pattern;
    void        (*call_match)(bool);
    void        (*call_mismatch)(bool);
} control_struct;

const control_struct control_arr[] = {
    {0b0001, subroutine_a, subroutine_a},
    {0b0011, subroutine_b, subroutine_b},
    {0b0100, subroutine_a, subroutine_b},
    {0b1000, NULL,         subroutine_c},
};

Given the structure above you can defer the call between "match" and "mismatch" or discharge a call by using "NULL" in that array...: https://godbolt.org/z/e4YG5jMqj.

toybaer
  • 136
  • 4