|Home| |Programming| |RTOS| |Embedded System| |Health| |DVB| |Tamil|
Home Page

Embedded C Questions And Answers

Write a macro for set and reset, swap.
#define SET_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) | _NO_
#define RESET_BIT( _X_, _NO_ ) ~( ( 1<<(_X_-1) ) ) & _NO_
#define SWAP_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) ^ _NO_

how to find give no is 2 power of n?
if ( ( no & ( no-1 ) ) == 0 )
printf( "Given number is 2 Power of N\n" );
else
printf( "Given number is not 2 Power of N\n" );

Swape two numbers without using third variable.
x^= y^= x^= y;

volatile int* ptr;
int* volatile ptr;
what is the difference?

volatile int* ptr; - Pointer is volatile.
int* volatile ptr; - Actual address stored in the pointer is volatile.

Assume that 0x7600 is an address. How will you store 50 in that address?
*( ( int * ) 0x7600 ) = 50;

Difference between memcpy and memmove.
memmove offers guaranteed behavior if the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove.

How to find the given number is little endian or big endian?
#include <stdio.h>
int main()
{
  unsigned int n = 1;
  char *p;
  p = (char*)&n;
  if (*p == 1)
  printf("Little Endian\n");
  else if (*(p + sizeof(int) - 1) == 1)
  printf("Big Endian\n");
  else
  printf("Surprise output!!!!\n");
  return 0;
}

Reverse a single linked list
Node *Reverse (Node *p)
{
  Node *pr = NULL;
  while (p != NULL)
  {
  Node *tmp = p->next;
  p->next = pr;
  pr = p;
  p = tmp;
  }
  return pr;
}

Finding Loop in a single linked list.
(1) If the linked list is read only, take two pointer approach( p1, p2). Both pointing to beginning of linked list. Now increment p1 by 1 and p2 by 2 and compare both. if they are equal there is a cycle. Repeat this untill p2 points to null.
(2) If you have the condition not to modify the node but you can change the links, then reverse the linked list. If you reach the head node then there is a cycle.

Finding middle of the single linked list in a single traversl.
Step 1:
Take two pointers P1 and P2, both pointed to the first element.
Step 2:
Increment P1 by 1 and P2 by two.
Step 3:
Whenever P2 reaches to the end, P1 will be at the middle of the list, just return P1->data.

Nibble Swap And Bit Swapping
int main( void )
{
  unsigned char a = 40, b=20;
  a = ( a>>4 ) | ( a<<4 );
  b = ( ( b & 0xAA ) >> 1 ) | ( ( b & 0x55 ) << 1 );
  clrscr();
  printf( "After Nibble Swap %d\n", a );
  printf( "Bit swapping %d\n", b );
  getch();
  return 0;
}

C program to count the number of set bits in an unsigned integer
/*
Program to count no. of bits in an unsigned integer
*/
void main( void )
{
unsigned int a = 15;
int count = 0;

while( a )
{
++count;
a = a & ( a - 1 );
}

clrscr();
printf( "Count is %d\n", count );
getch();
}

Why is sizeof('a') not 1?
Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though it's different in C++).
Result:
In Turbo C output is: 2
In Turbo C++ output is: 1

Determine if a Variable is Signed or Not?
Using Typecasting
#define IS_SIGNED( _typedef_ ) ( (_typedef_) 0-1 > 0 )
Using 1's Compliment
#define IS_SIGNED2(__VAR__) ( ( __VAR__ > 0 ) && ( ~__VAR__ > 0 ) )

why n++ executes faster than n+1?
The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.

Little endian to big endian
//2-byte number
int SHORT_little_endian_to_big_endian( int i)
{
return (( i>>8)&0xff)+((i<<8)&0xff00);
}
//4-byte number
int INT_little_endian_To_big_endian(int i)
{
return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
}