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" );
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?
unsigned int n = 1;
p = (char*)&n;
if (*p == 1)
else if (*(p + sizeof(int) - 1) == 1)
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;
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.
Take two pointers P1 and P2, both pointed to the first element.
Increment P1 by 1 and P2 by two.
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 );
printf( "After Nibble Swap %d\n", a );
printf( "Bit swapping %d\n", b );
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 )
a = a & ( a - 1 );
printf( "Count is %d\n", count );
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++).
In Turbo C output is: 2
In Turbo C++ output is: 1
Determine if a Variable is Signed or Not?
#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
int SHORT_little_endian_to_big_endian( int i)
return (( i>>8)&0xff)+((i<<8)&0xff00);
int INT_little_endian_To_big_endian(int i)