Skip to main content

Embedded C tricky Question

 

Embedded C Programming

1. Write an Embedded C program to multiply any number by 9 in the fastest manner.

This can be achieved by involving bit manipulation techniques - Shift left operator as shown below:

#include<stdio.h>
void main(){
    int num;
    printf(“Enter number: ”);
    scanf(“%d”,&num);
    printf(“%d”, (num<<3)+num);
}

2. How will you swap two variables? Write different approaches for the same.

  • Using Extra Memory Space:
int num1=20, num2=30, temp;
temp = num1;
num1 = num2;
num2 = temp;
  • Using Arithmetic Operators:
int num1=20, num2=30;
num1=num1 + num2;
num2=num1 - num2;
num1=num1 - num2;
  • Using Bit-Wise Operators:
int num1=20, num2=30;
num1=num1 ^ num2;
num2=num2 ^ num1;
num1=num1 ^ num2;
  • Using One-liner Bit-wise Operators:
int num1=20, num2=30;
num1^=num2^=num1^=num2;

The order of evaluation here is right to left.

  • Using One-liner Arithmetic Operators:
int num1=20, num2=30;
num1 = (num1+num2)-(num2=num1);

Here the order of evaluation is from left to right.

3. Write a program to check if a number is a power of 2 or not.

We can do this by using bitwise operators.

void main (){
    int num;
    printf ("Enter any no:");
    scanf ("%d", &num);
    if (num & & ((num & num-1) == 0))
        printf ("Number is a power of 2");
    else
        printf ("Number is not a power of 2");
}

4. Write a program to print numbers from 1 to 100 without making use of conditional operators?

void main (){
   int i=0;
   while (100 – i++)
   printf ("%d", i);
}

5. Write a MIN macro program that takes two arguments and returns the smallest of both arguments.

#define MIN(NUM1,NUM2) ( (NUM1) <= (NUM2) ? (NUM1) : (NUM2) )

Comments

Popular posts from this blog

Questions

1. What is a Stub function? Ans : A function without any definition which presents no error when called. 2. Why there are two ld scripts generated by STM32Cube IDE? Ans: The CubeIDE always presents two different LDscripts on for generating the executable for Debugging which goes to RAM another for normal code upload that goes to flash memory. 3. What is Supervisory Mode/ privileged mode ? Ans : When you are using a Kernel then there are two modes user mode and privileged mode. In User mode the applications cant have system calls. 4. What is "make -j6" ? Ans : Make has an argument for number of jobs, so when you add -j6 it will create 6 different compiler instances, so that the systems could use multicore to the fullest.  Linux people generally do add "-j $(nproc)" where nproc is a command line which returns number of processors attached. 5. What is weak attribute ? Ans : Weak attribute used to denote weak symbols which helps linkers to choose one function out of mul...

#@$%

  #include  "msp430x21x2.h"     #define PWM_OUTPUT              0x03 #define DUTY_CYCLE              TA0CCR2 #define MAX_LIMIT               300   /* #define no_of_samples           1 #define duty_cycle_limit        1000 #define temp_sensor             0xA000   #define panel_selection_cutoff  250 */ #define PV_VOLTAGE              INCH_0 #define PV_CURRENT              INCH_1 #define BATT_VOLTAGE            INCH_2 #define BATT_CURRENT            INCH_3   #define UPPER_CRG_LVL           0x0105 // 0x00FF // 0x00F5 //0x023A #define LOWER_CRG_LVL   ...

STM32 Port configuration via structures

 How Does STM32CubeIDE   /*Configure GPIO pin : Pd3 */   GPIO_InitStruct.Pin = GPIO_PIN_3;   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;   GPIO_InitStruct.Pull = GPIO_NOPULL;   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;   HAL_GPIO_Init(GPIOd, &GPIO_InitStruct);  Now what is GPIOD and GPIO_InitStruct GPIOD the definition is deep inside CMSIS "Drivers\CMSIS\Device\ST\STM32F4xx\Include\stm32f411xe.h" #define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE) and  GPIOD_BASE declared in same file #define GPIOD_BASE            (AHB1PERIPH_BASE + 0x0C00UL) Now GPIO_InitStruct is comes from  the line GPIO_InitTypeDef GPIO_InitStruct = {0};  so the structure GPIO_InitTypeDef is as follows as defined in  typedef struct {   uint32_t Pin; /*!< Specifies the GPIO pins to be configured.     This parameter can be any value of @ref GPIO_p...