C PROGRAM ON BITWISE OPERATION

C Program to Perform Addition using Bitwise Operators

#include<stdio.h>

 

int bitwiseadd(int x, int y)

{

    while (y != 0)

    {

        int carry = x & y;

        x = x ^ y;

        y = carry << 1;

    }

    return x;

}

 

int main()

{

    int num1, num2;

    printf("\nEnter two numbers to perform addition using bitwise operators: ");

    scanf("%d%d", &num1, &num2);

    printf("\nSum is %d", bitwiseadd(num1, num2));

    return 0;

}

$ gcc bitwiseadd.c -o bitwiseadd

$ ./bitwiseadd

 

Enter two numbers to perform addition using bitwise operators: 20 12

Sum is 32

C Program to Check whether nth Bit is Set or not

/*
 * C Program to Check if nth Bit in a 32-bit Integer is Set or not
 */
#include <stdio.h>
 
/* gloabal varaibles */
int result,position;
/* function prototype */
int n_bit_position(int x,int position);
 
void main()
{
    unsigned int number;
 
    printf("Enter the unsigned integer:\n");
    scanf("%d", &number);
    printf("enter position\n");
    scanf("%d", &position);
    n_bit_position(number, position);
    if (result & 1)
        printf("YES\n");
    else
        printf("NO\n");
}
 
/* function to check whether the position is set to 1 or not */
int n_bit_position(int number,int position)
{
    result = (number>>(position));
}

Runtime Test Cases

 

$ cc bit32.c

$ a.out

Enter the unsigned integer:

101

enter position

4

NO

 

$ a.out

Enter the unsigned integer:

113

enter position

4

YES

C Program to Find the Highest Bit Set for any Given Integer

*
 * C Program to find the Highest Bit Set for any given Integer
 */
#include <stdio.h>
#define NUM_BITS sizeof(int)*8
 
int highest_bit_set(int);
void display(int);
int i = NUM_BITS;
 
void main()
{
    int num, pos;
 
    printf("\nenter the number:");
    scanf("%d", &num);
 
    display(num);
    pos = highest_bit_set(num);
    printf("\nthe position of the highest bit set is %d", pos);
}
/* RETURNS THE POSITION */
int highest_bit_set(int num)
{
    int count = 0;
    while (num >> 1 != 0)
    {
        count++;
        num = num >> 1;
    }
    return(count);
}
/* DISPLAYS THE NUMBER IN BINARY REPRESENTATION */
void display(int num)
{
    int c;
    c = num & 1;
    if (i > 0)
    {
        i--;
        display(num >> 1);
    }
    printf("%d", c);
}

Runtime Test Cases

 

$ cc bit17.c

$ a.out

enter the number:10000

000000000000000000010011100010000

the position of the highest bit set is 13

C Program to Find MSB Position using Function

/*
 * C Program that uses Function to return MSB position of unsigned Integer
 */
#include <stdio.h>
#define NUM_BITS_INT 32
int int_msb_position(int n);
 
void main()
{
    int n, pos;
 
    printf("Enter a number : ");
    scanf("%d", &n);
    pos = int_msb_position(n);
    printf("\nPosition of MSB bit = %d\n", NUM_BITS_INT - (pos + 1));
}
 
/* Function to find the MSB bit position */
int int_msb_position(int n)
{
    int i = 0, bit;
    while (i < NUM_BITS_INT)
    {
        bit = n & 0x80000000;
        if (bit == -0x80000000)
           {
            bit = 1;
        }
           if (bit == 1) 
            break;
        n = n << 1;
        i++;
    }
    return i;
}
$ cc bit24.c
$ a.out
Enter a number : 127
 
Position of MSB bit = 6
 
Enter a number : 259
 
Position of MSB bit = 8
 
Enter a number : 5
 
Position of MSB bit = 2