C-Macros-to-do-Bitwise-Operations

Sl.No

Description

Code

1

Macro to set a bit at a position

#include <stdio.h>
#define SET_BIT(num, pos)(num |= (1 << pos))

void print_bit(int num)
{
        int i = 1;
        printf("LSB -> [");
        while (i != i<<31) {
                i & num?printf("1"):printf("0");;
                i <<= 1;
        }
        printf("]<-MSB");
        printf("\n");
}

int main(void)
{
        unsigned int num = 0x0;
        int pos = 5;

        /*Before SET_BIT*/
        print_bit(num);

        SET_BIT(num, pos);

        /*After SET_BIT*/
        print_bit(num);

        return 0;
}

2

Macro to clear a bit at a position

#include <stdio.h>

#define CLEAR_BIT(num, pos)(num &= ~(1 << pos))

int main(void)
{
        unsigned int num = 0xffffffff;
        int pos = 5;

        /*Before CLEAR_BIT*/
        print_bit(num);

        CLEAR_BIT(num, pos);

        /*After CLEAR_BIT*/
        print_bit(num);

        return 0;
}

3

Macro to find maximum of 2 number

#include <stdio.h>

#define FIND_MAX(num1, num2) (num1 > num2 ? num1: num2)

int main(void)
{
        int num1 = 5;
        int num2 = 2;


        printf("Greater of %d and %d is : %d\n",
                        num1, num2, FIND_MAX(num1, num2));


        return 0;
}

4

Macro to find a minimum of 2 number

#include <stdio.h>

#define FIND_MAX(num1, num2) (num1 < num2 ? num1: num2)

int main(void)
{
        int num1 = 5;
        int num2 = 2;


        printf("Smaller of %d and %d is : %d\n",
                        num1, num2, FIND_MAX(num1, num2));


        return 0;
}

5

Macro to swap bit b/w source and destination bit of a number

#include<stdio.h>

#define BIT_SWAP(n, s, d) ((((1 << s) & n) >> s) ^ (((1 << d) & n) >> d)? ((n ^= ((1 << s) | (1 << d)))):n)

void print_bit(int num)
{
        int i = 1;
        printf("LSB -> [");
        while (i != i<<31) {
                i & num?printf("1"):printf("0");;
                i <<= 1;
        }
        printf("]<-MSB");
        printf("\n");
}

int main(void)
{
	int n = 0xfffffff0;
	int s = 0;
	int d = 4;
	
	print_bit(BIT_SWAP(n, s, d));

	return 0;
}

6

Macro to left rotate the bits

#include<stdio.h>

#define LEFT_ROT_BITS(num) (num & (1 << ((sizeof(int) * 8) - 1)) ? num = ((num << 1) | 1): (num<<=1))

int main(void)
{
        unsigned int num = 1;
        print_bits(num);
        print_bits(LEFT_ROT_BITS(num));
        print_bits(LEFT_ROT_BITS(num));
        print_bits(num);

        return 0;
}

7

Macro to right rotate the bits

#include<stdio.h>

#define RIGHT_ROT_BITS(num) (num & 1? num = ((num >> 1) | 1 << ((sizeof(int)*8) - 1)): (num >>= 1))

int main(void)
{
        unsigned int num = 15;
        print_bits(num);
        print_bits(RIGHT_ROT_BITS(num));
        print_bits(RIGHT_ROT_BITS(num));
        print_bits(num);

        return 0;
}

8

Macro to toggle a bit

#include<stdio.h>

#define TOGGLE_BIT(num, pos) (num ^= (1 << pos))

int main(void)
{
	int num = 15;
	int pos = 2;
	
	print_bit(num);
	print_bit(TOGGLE_BIT(num, pos));
	
	return 0;
}

9

Macro to check whether bit is set or clear

#include<stdio.h>

#define CHK_BIT(num, pos) (num & (1 << pos))

int main(void)
{
	int num = 15;
	int pos = 3;
	
	print_bit(num);
	printf("The bit at pos %d is %d\n", 
	             pos, CHK_BIT(num, pos)?1:0);
	
	return 0;
}

10

Macro to swap 2 half word

#include<stdio.h>

#define SWAP_HALF_WORD(num) (num = ((num & 0x0000FFFF) << 16)\
|((num & 0xFFFF0000) >> 16))

int main(void)
{
	int num = 0xffff0000;
	
	print_bit(num);
	SWAP_HALF_WORD(num);
	print_bit(num);

	
	return 0;
}

11

Macro to find the endianness

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

static int tmp = 0xffffff00;

#define IS_L_ENDIAN(tmp) (*(char *)&tmp == 0x00 ? 1:0)

int main(void)
{
        system("lscpu | grep -i 'endian'\n");
        printf("Current machine is  : %s\n",
                        IS_L_ENDIAN(tmp)?"Little": "Big");

        return 0;
}

12

Function to toggle even bits

#include<stdio.h>

int even_bit_toggle(unsigned int num)
{
	int st = 0;

	while (st < (sizeof(int) * 8)) {
		if (!(st % 2))
			num = num ^ (1 << st);
		st++;
	}

	return num;
}

int main(void)
{
	print_bit(even_bit_toggle(0xffffffff));	
	return 0;
}

13

Function to toggle odd bits

#include<stdio.h>

int odd_bit_toggle(unsigned int num)
{
	int st = 0;

	while (st < (sizeof(int) * 8)) {
		if ((st % 2))
			num = num ^ (1 << st);
		st++;
	}

	return num;
}

int main(void)
{
	print_bit(odd_bit_toggle(0xffffffff));	
	return 0;
}

14

Function to left rotate bits “N” times

#include<stdio.h>

unsigned int left_rotate_n_bits(unsigned int num, int n)
{
	while (n--)
		if (num & (1 << ((sizeof(int) * 8) - 1)))
			num = (num << 1) | 1;
		else 
			(num<<=1);
	return num;
}

int main(void)
{
	unsigned int num = 14;
	int n = 3;
	
	print_bits(num);
	print_bits(left_rotate_n_bits(num, n));

	return 0;
}

15

Function to right rotate bits “N” times

#include<stdio.h>

unsigned int right_rotate_n_bits(unsigned int num, int n)
{
	while (n--)
		if (num & 1)
			num = (num >> 1) | 1 << ((sizeof(int)*8) - 1);
		else 
			(num >>= 1);
	return num;
}

int main(void)
{
	unsigned int num = 14;
	int n = 3;
	
	print_bits(num);
	print_bits(right_rotate_n_bits(num, n));

	return 0;
}

16

Function to count set bits

#include<stdio.h>

int setbit_cnt(int num)
{
	int i = 0;
	int bit_cnt = 0;

	while (i < (sizeof(int) * 8)) {
		if (num & (1 << i))
			bit_cnt++;
		i++;
	}

	return bit_cnt;
}


int main(void)
{
	int num = 12;

	printf("Setbit count is = %d\n", setbit_cnt(num));	

	return 0;
}

17

Function to count clear bit

#include<stdio.h>

int clearbit_cnt(int num)
{
	int i = 0;
	int bit_cnt = 0;

	while (i < (sizeof(int) * 8)) {
		if (!(num & (1 << i)))
			bit_cnt++;
		i++;
	}

	return bit_cnt;
}

int main(void)
{
	int num = 12;

	printf("Clearbit count is = %d\n", clearbit_cnt(num));	

	return 0;
}

18

Function to count leading set bits

#include<stdio.h>

int cnt_leading_set_bits(int num)
{
	unsigned int mk;
	int cnt = 0;
	mk = 1 << ((sizeof(int) * 8) - 1);

	while (mk & num && mk > 0) {
		mk >>= 1;
		cnt++;
	}	

	return cnt;	
}

int main(void)
{
	int num = 0xf0000000;	

	print_bit(num);
	printf("No of Leading setbits are %d\n",
	             cnt_leading_set_bits(num));



	return 0;
}

19

Function to count leading clear bits

#include<stdio.h>

int cnt_leading_cleared_bits(int num)
{
	unsigned int mk;
	int cnt = 0;
	mk = 1 << ((sizeof(int) * 8) - 1);

	while (!(mk & num) && mk > 0) {
		mk >>= 1;
		cnt++;
	}	

	return cnt;	
}

int main(void)
{
	int num = 0x00000001;	

	print_bit(num);
	printf("No of Leading clear bits are %d\n",
	             cnt_leading_cleared_bits(num));

	return 0;
}

20

Function to count trailing set bits

#include<stdio.h>

int cnt_trailing_set_bits(int num)
{
	unsigned int mk;
	int cnt = 0;
	mk = 1;

	while ((mk & num) && mk > 0) {
		mk <<= 1;
		cnt++;
	}	

	return cnt;
}

int main(void)
{
	int num = 0x00000001;	

	print_bit(num);
	printf("No of Trailing setbits are %d\n",
	             cnt_trailing_set_bits(num));



	return 0;
}

21

Function to count trailing clear bits

#include<stdio.h>

int cnt_trailing_cleared_bits(int num)
{
	unsigned int mk;
	int cnt = 0;
	mk = 1;

	while (!(mk & num) && mk > 0) {
		mk <<= 1;
		cnt++;
	}	

	return cnt;
}

int main(void)
{
	int num = 0x00000004;	

	print_bit(num);
	printf("No of Trailing clear bits are %d\n",
	             cnt_trailing_cleared_bits(num));



	return 0;
}

22

Clear rightmost set bits

#include<stdio.h>

#define CLR_RM_SET_BIT(num) (num = num & (num -1))

int main(void)
{
	int num = 15;
	
	print_bit(num);
	print_bit(CLR_RM_SET_BIT(num));
	
	return 0;
}

23

Set left most mist clear bit

#include<stdio.h>

int set_lmost_clearbit(unsigned int num)
{
	int i = 0;

	while (i < (sizeof(int)*8)) 
		if (((num >> i) & 1)) {
			i++;
		} else {
			num = (num | (1 << i));
			return num;
		}

	return num;
}

int main(void)
{
	int num = 15;
	
	print_bit(num);
	print_bit(set_lmost_clearbit(num));
	
	return 0;
}

24

Function swap 2 number using bitwise operator

#include<stdio.h>

void swap_num(int *num1, int *num2)
{
	*num1 ^= *num2;
	*num2 ^= *num1;
	*num1 ^= *num2;
}

int main(void)
{
	int num1 = 5;
	int num2 = 3;

	printf("Before : \"A\" is : %d, \"B\" is %d\n", num1, num2);
	
	swap_num(&num1, &num2);

	printf("After  : \"A\" is : %d, \"B\" is %d\n", num1, num2);

	return 0;
}