Header Ads

Embedded C Interview Questions with Answers | Part 3

Swap Adjacent Even & Odd Bits of Any Type of Variable in C

#include "stdafx.h"
#include <conio.h>

#include <stdio.h>


int bitMask1[8]= {

0xaa,
0xaaaa,
0xaaaaaa,
0xaaaaaaaa,
0xaaaaaaaaaa,
0xaaaaaaaaaaaa,
0xaaaaaaaaaaaaaa,
0xaaaaaaaaaaaaaaaa};

int bitMask2[8]= {
0x55,
0x5555,
0x555555,
0x55555555,
0x5555555555,
0x555555555555,
0x55555555555555,
0x5555555555555555};

int swapAdjBits(int x)

{
    return (((x & (bitMask1[sizeof(x)-1])) >> 1) | ((x & (bitMask2[sizeof(x)-1])) << 1));
}

#define SWAP_ADJ_BITS(x) (((x & (bitMask1[sizeof(x)-1])) >> 1) | ((x & (bitMask2[sizeof(x)-1])) << 1))



int _tmain(int argc, _TCHAR* argv[])

{
    printf("Hello, World!\n");
    char x = 23;
    int y = 23;

    long z = 23;

    printf("%d\n",swapAdjBits(x));
    printf("%d\n",SWAP_ADJ_BITS(x));
    printf("%d\n",swapAdjBits(y));
    printf("%d\n",SWAP_ADJ_BITS(y));
    printf("%d\n",swapAdjBits(z));
    printf("%d\n",SWAP_ADJ_BITS(z));
    getch();

    return 0;

}


How to toggle particular bit in given number 


There are many ways & everyone has different thinking to do the same. I would like to demonstrate my way to do the same. For the same first you need to have the clear understanding of bit-wise operation in C. You need to know what is "bit wise AND" & what is "logical AND" operation.

  • && Means Logical AND
  • & means Bitwise AND
Program:
______________________________
#include <stdio.h>
#define MSB (1 << ((sizeof(int)*8)-1))
#define BITPOS(pos) (1 << pos)
#define MAXVAL (MSB | (MSB-1))
int toggleBit(int num, char pos)
{
int temp = (num & (BITPOS(pos)));
temp = ~temp;
//printf("%x\n%x\n",temp, MAXVAL); //To debug the code
if(temp != MAXVAL)
{
num = num & temp;
}
else
{
temp = (temp & (BITPOS(pos)));
num = num | temp;
}
//printf("%x\n",num); //To debug the code
return num;
}
int main() {
int ProgBits = 0xFF08; //Number with Programmable Bits
char bitposition = 2;
printf("Given number: %x\n",ProgBits);
ProgBits = toggleBit(ProgBits, bitposition); //Toggle particular bit in a given number
printf("Number with toggle bit position %d: %x\n",bitposition, ProgBits);
return 0;
}                                                                                                                                                                                                                                                                                                                                                           Output:                                                                                                                                                             Given number: ff08
Number with toggle bit position 2: ff0c

Swap Two Bits in a Number of Any Type | Swap Bit position

#include <stdio.h>
#define BIG_MASK(x,y)       (1 << ((x > y) ? x:y))
#define SMALL_MASK(x,y)     (1 << ((x > y) ? y:x))
#define BIT_DIFF(x,y)       (x-y)
#define SWAP_BITS(num,x,y) ((num & ~(BIG_MASK(x,y) | SMALL_MASK(x,y)))  |       \
                            ((num & BIG_MASK(x,y)) >> BIT_DIFF(x,y))    |       \
                            ((num & SMALL_MASK(x,y)) << BIT_DIFF(x,y)))
                            
//Example:
//x = (x & ~0x14) | ((x & 0x10) >> 2) | ((x & 0x04) << 2);

int main()
{
    printf("Hello, World!\n");
    int a = 0x30;
    int bitpos1 = 5;
    int bitpos2 = 2;
    printf("0x%x\n",a);
    printf("0x%x\n",BIG_MASK(bitpos1,bitpos2));
    printf("0x%x\n",SMALL_MASK(bitpos1,bitpos2));
    printf("0x%x\n",SWAP_BITS(a,bitpos1,bitpos2));

    return 0;
}

Output:

Hello,World!                                                                                                                                                      
0x30                                                                                                                                                                   
0x20                                                                                                                                                                     
0x4                                                                                                                                                                      
0x14


How to Set Range of Bits in Given Number in C 



// StringTest.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"

//#define SETBIT(num, start_bit_pos, end_bit_pos) (num) | ((1<<(((end_bit_pos)-(start_bit_pos))+1))-1)<<((start_bit_pos)-1)
#define SETBIT(n,a,b) (n | (((((1 << (a + 1 - b)) - 1)) << (b))))

void printbin(int pnum){

    if(pnum != 0)
    {
        printbin(pnum >> 1);
   
        if (pnum & 1){
        printf("1");
        }
        else{
        printf("0");
        }
    }
    else{
   
    }
    //    pnum >>= 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int num = 0x10;
    int startPos = 0;
    int endPos = 3;

    printf("Before Number = ");
    int printnum = num;
    printbin(printnum);
    printf("\n");

    /*while (printnum) {

        if (printnum & 1){
        printf("1");
        }
        else{
        printf("0");
        }

        printnum >>= 1;
    }
    printf("\n");*/

    int mask = ((((1 << (endPos + 1-startPos)) -1)) << (startPos));
    printf("Mask for Number = ");
    printbin(mask);
    printf("\n");

    /*printnum = mask;
    while (printnum) {

    if (printnum & 1)
        printf("1");
    else
        printf("0");

        printnum >>= 1;
    }
    printf("\n");*/

    num = SETBIT(num, endPos, startPos);
    printf("After Number = ");

    printbin(num);
    printf("\n");

    /*printnum = num;
    while (printnum) {

    if (printnum & 1)
        printf("1");
    else
        printf("0");

        printnum >>= 1;
    }
    printf("\n");*/

    getch();
    return 0;
}