Embedded C Interview Questions Part -2

BIT MANIPULATIONS:

1) WRITE A PROGRAM TO SET BIT, CLEAR BIT, TOGGLE BIT OPERATIONS IN EMBEDDED C.

#include "stdio.h"

void main()
{

  int num = 182;      //Binary is 1011 0110

  //To set 3rd bit

  num|= 1<<3;
  
  printf("%b",num);   //Output shall be 1011 1110

  //To clear 3rd bit 

  num&=~(1<<3);
  
  printf("\n%b",num);   //Output shall be 1011 0110

  //To toggle 3rd bit

  num^=(1<<3);
  
  printf("\n%b",num);   //Output shall be 1011 1110

}

2) WRITE A PROGRAM TO SET BIT, CLEAR BIT, TOGGLE BIT OPERATIONS USING MACRO FUNCTION EMBEDDED C.

#include "stdio.h"

#define SET_BIT(num, pos) num|= (1U<< pos)       // Macro function for set bit
#define CLEAR_BIT(num, pos) num&= ~(1U<< pos)    // Macro function for clear bit
#define TOGGLE_BIT(num, pos) num^= (1U<< pos)    // Macro function for toggle bit

void main()
{

  int num = 182;                     //Binary is 1011 0110

  //To set 3rd bit
  
  printf("%b",SET_BIT(num,3));       //Output shall be 1011 1110

  //To clear 3rd bit 
  
  printf("\n%b",CLEAR_BIT(num,3));   //Output shall be 1011 0110

  //To toggle 3rd bit
  
  printf("\n%b",TOGGLE_BIT(num,3));  //Output shall be 1011 1110

}

3) WRITE A PROGRAM TO CHECK BIT IS SET OR NOT EMBEDDED C.

#include "stdio.h"

void main(){
int num,pos;
printf("Enter the number and position to check bit:");
scanf("%d %d",&num,&pos);

int chkbit = num & (1<<pos);           // Logic to check set bit

if(chkbit)
  printf("The bit is set");
else
  printf("The bit is not set");

}

4) WRITE A PROGRAM TO SWAP 2 NIBBLES IN A BYTE EMBEDDED C.

#include "stdio.h"

unsigned char swapNibbles(unsigned char x)    
{
  return( (x&0x0F)<<4 | (x&0xF0)>>4 );     // Function to swap nibbles in byte
}

void main(){
    
    int num= 0x12;
    num = swapNibbles(num);
    
    printf("%x",num);                     //Output is 0x21
    
}

5) WRITE A PROGRAM TO CONVERT LITTLE ENDIAN TO BIG ENDIAN EMBEDDED C.

#include "stdio.h"

// Function to perform Little Endian To Big Endian Conversion

unsigned int LTB(unsigned int x)     
{
  return(((x>>24)&0x000000FF) | ((x>>8)&0x0000FF00) | ((x<<8)&0x00FF0000) | ((x<<24)&0xFF000000));
}

void main(){
    
    int num= 0x12345678;
    num = LTB(num);
    
    printf("%x",num);        //Output is 0x78563412
}

6) WRITE A PROGRAM TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE EMBEDDED C.

#include "stdio.h"

void main(){
    
    int x=4, y=6;

    x = x+y;    // x now becomes 10
    y = x-y;    // y becomes 4
    x = x-y;    // x becomes 6
    
    printf("x=%d y=%d", x,y);      //Output is x=6 y=4

          //OR//

    x = x^y;
    y = x^y;   // x = 4
    x = x^y;   // y = 6
    
    printf("\nx=%d y=%d", x,y);   //Output is x=4 y=6
    
}

7) What is memory leak in Embedded C?

Memory leak occurs when programmer creates a memory and forgets to delete it.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *ptr = (int*) malloc(sizeof(int));

    return 0;                          // Return without freeing the memory
}

8) What is the different between pre-increment and post increment in embedded C?

Pre-increment (i++):

  1. Returns old value & then increments “i”.
  2. Associativity is from left to right.

See below example-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=1;
    printf("%d",i++);        // Output shall be 1

    return 0;                                    
}

Post-increment (i++):

  1. Increments old value and returns new value.
  2. Associativity is from right to left .

See below example-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=1;
    printf("%d",++i);        // Output shall be 2

    return 0;                                    
}

Note: Precedence of postfix is higher than prefix.

9) What is volatile keyword in Embedded C?

The volatile keyword is used in Embedded C when the value of a variable keeps updating from hardware. The volatile keyword thus instructs the compiler to not to optimize the variable since its value keeps changing by other means (for example through hardware) rather than code.

10) What is volatile constant keyword in Embedded C?

The volatile constant keyword is used when the value of variable is intended to be changed by other code or hardware or pointer in the same code.

Volatile Constant would be used where tow processors are communicating via. a shared memory area.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    volatile const int i =100;
    
    int *ptr = &i;
    
    printf("%d",i);         // Output is 100
    
    *ptr = 200;
    
    printf("\n%d",i);      // Output is 100
    
    i = 300;
    
    printf("%d",i);        // will output error
    
    return 0;                                    
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like