C Programs – (Bit Operations)

1. Find the number of set bits of a given number

Write a program which takes an integer value as an input and finds the number of set bits (i.e 1s) in that variable
sample input 1: 54
output: 4

sample input 2: 1024
output: 1

Explanation: For the input 1 the value is 54, its binary equivalent is 110110 so the number of 1s are 4.
For the input 2 value is 1024, its binary equivalent is 10000000000. so the number of 1s are 1.

Program:

#include <stdio.h>

int main()
{
        int num;
        int i;
        int cnt = 0;

        printf("Enter number:");
        scanf("%d", &num);

        for (i = 0; i < 31; i++) {
                if ((num >> i) & 0x1)
                        cnt++;
        }
        printf("The total set bits are %d\n", cnt);

}

2. Set, Clear and toggle a given bit for a given number

Write a program which takes an integer value as an input, program have to do three different operations for that number
1. Set a bit: if a bit position in the number is 0 make it 1.
2. Clear a bit: if the bit position in the that number is 1 make it 0.
3. toggle a bit: convert the state of the bit position, if 1 make 0, if 0 make 1.
ask your about the bit position. (position starts from zero)

sample input1: number= 34 , bit position= 3.
output: set value=42, clear value=34, toggle value=42.

sample input2: number= 5, bit position= 1
output: set value=7, clear value:=5, toggle value=7.

Program:

#include <stdio.h>

int main()
{
        int num;
        int i;
        int pos = 0;
        int mask;

        printf("Enter number:");
        scanf("%d", &num);
        printf("enter a bit position:");
        scanf("%d", &pos);

        mask = 1 << pos;
        printf("number after setting position %d is %d\n", pos, num | mask);
        printf("number after clearing position %d is %d\n", pos, num & (~mask));
        printf("number after toggling position %d is %d\n", pos, num ^ mask);
}

3. Set, Clear and toggle n bits for a given number from position p

sample input1: number in hex = 0x45789
no. of bits = 4
position = 8

output: set value = 0x4f789
clear value = 0x40789
toggle value = 0x4a789

Explanation: position starts from 8, till 11. so the bits 8,9,10 and 11 of the given number have to clear, set and toggle at that bits 5 is the hex value and binary value is 0101, if cleared 0000 i.e 0 in hex. if set, 1111 i.e f in hex. if toggled 1010 i.e A in hex. rest of the bits remain untouched.

sample input2: number = 0xc0de
no. of bits = 7
position = 5

output: clear value = 0xc01e
set value = 0xcffe
toggle value = 0xcf3e

Program:

#include <stdio.h>

int main()
{
        int num;
        int b, pos;
        int mask;
        int result;

        printf("Enter a number in hex:");
        scanf("%x", &num);
        printf("enter the number of bits:");
        scanf("%d", &b);
        printf("enter the starting position:");
        scanf("%d", &pos);

        /* create mask value */
        mask = ~(~0 << b) << pos;

        /* set bits */
        result = num | mask;
        printf("value after setting: ");
        printf("0x%x\n", result);

        /* clear bits */
        result = num & ~mask;
        printf("value after clearing:\n");
        printf("0x%x\n", result);

        /* toggle */
        result = num ^ mask;
        printf("value after toggling:\n");
        printf("0x%x\n", result);
}

input:

Enter a number in hex: 1de9
enter the number of bits: 4
enter the starting position: 2

output:

value after setting: 0x1dfd
value after clearing: 0x1dc1
value after toggling: 0x1dd5

4. Reverse the digits of a given Hex value.

input: 0x12345678
output: 0x87654321

#include <stdio.h>

int main()
{
        int x = 0x12345678;
        int y = 0;
        int i;
        int mask = 0xf;
       
        for (i = 0; i < 8; i ++) {
                   nibble = (x >> (i * 4)) & mask;
                   y | = nibble << ((7 - i) * 4);
        }
        printf("y = 0x%x", y);

}

output:

y = 0x87654321

5. Reverse the bits of a given number

sample input: 4 (00000000000000000000000000000100)
output : 00100000000000000000000000000000

#include <stdio.h>

void print_binary(int val)
{
        int i;

        for (i = 31; i >= 0; i--)
                (val >> i) & 0x1 ? printf("1"): printf("0");
        
        printf("\n");
}

int main()
{
        int num;
        int i;
        int mask;

        printf("Enter a number:");
        scanf("%d", &num);

        print_binary(num);

        for (i = 0; i <= 31/2; i++) {
                if (((num >> i) & 0x1) != ((num >> (31-i)) & 0x1)) {
                        num ^= 1 << i;
                        num ^= 1 << 31-i;
                }
        }
        printf("binary value after conversion:\n");
        print_binary(num);
}

input:

65567

output:

00000000000000010000000000011111
binary value after conversion:
11111000000000001000000000000000

6. Replace n bits from position p of a variable x with y

sample input: x= 0x4567, y = 0xf0ff, no of bits = 4, position = 8
output: 0x4067

Explanation: the 4 bits from 8 to 11 in x is to be replaced with the bit values from 8 to 11 y.
0101 (5) is in x at that position, 0 is in y, after replacing the value of x becomes 0x4067

#include <stdio.h>

void print_binary(int val)
{
        int i;

        for (i = 31; i >= 0; i--)
                (val >> i) & 0x1 ? printf("1"): printf("0");
        
        printf("\n");
}

int main()
{
        int x, y;
        int b, pos;
        int mask;

        printf("Enter x value in hex:");
        scanf("%d", &x);
        printf("Enter y value in hex:");
        scanf("%d", &y);
        printf("enter the number of bits:");
        scanf("%d", &b);
        printf("enter the starting position:");
        scanf("%d", &pos);

        printf("x in binary:");
        print_binary(x);
        printf("y in binary:");
        print_binary(y);

        /* create mask value */
        mask = ~(~0 << b) << pos;

        y &= mask;
        x &= ~mask;

        x |= y;

        printf("X after replacement:");
        print_binary(x);

}

input:

Enter x value in hex: 0x4e3a
Enter y value in hex: a91e
enter the number of bits:3
enter the starting position:5

output:

value of x after replacement: 0x4e1a

7. Implement rotation of bits of a given number

when right shit or left shift happens to a number, the number of bits shifted to left/right is lost. for example lets take value 0x1f5468, if this value is right shifted by 4 times, the 4 LSBits are lost and the value becomes 0x1f546.

Write a program to rotate the bits right shifted to MSBits, and for left shifted to LSBits

sample input: 0x45689, right shift 4 times
output: 0x90004568

Program:

#include <stdio.h>

int main()
{
        int x,y;
        int n = 0;
        char dir;
        int mask;

        printf("Enter a number in hex:");
        scanf("%x", &x);

        printf("enter the number of bits to rotate:");
        scanf("%d", &n);
        /* clear input buffer */
        while ((getchar()) != '\n');
        printf("enter direction of rotation: right(r) or left(l):");
        scanf("%c", &dir);

        if (dir == 'l') {
                printf("the value after rotating left:");
                mask = ~0 << (32 - n);
                y = x & mask;
                y = y >> (32 -n);
                x = x << n;
                x |= y;
                printf("0x%x\n", x);

        } else if (dir == 'r') {
                printf("the value after rotating right:");
                mask = ~0 >> (32 - n);
                y = x & mask;
                y = y << (32 - n);
                x = x >> n;
                x |= y;
                printf("0x%x", x);

        } else {
                printf("invalid direction input:%c", dir);
        }
}

input:

Enter a number in hex:0x12345678
enter the number of bits to rotate:4
enter direction of rotation: right(r) or left(l):r

output:

the value after rotating right: 0x81234567