Featured Post

Trie implementation in C

BitWise Operators

This is a multi purpose bit operations program.
I have covered the most basic operations that can be performed using bitwise operators.

#include <stdio.h>
#include <stdlib.h>
#define CHAR_BIT 8         //No. of bits in a byte
#define BAD_INPUT_CUTOFF 3

int str[32];
enum {S=0,U};             //S-Set U-Unset

void showbits(unsigned int);

void toggle(int *inp)
{
    int mask = 0XFFFFFFFF;
    *inp = mask ^ *inp;
    showbits(*inp);
}

void detectset(unsigned int inp)
{
    int i , flag = 0;
    unsigned int mask = 0x80000000;
    int s = sizeof(inp) * CHAR_BIT - 1;

    for(i=0; i < s; i++)
    {
        if((mask & inp)!= 0 )
        {
            printf("%dth ,",i+1);
            flag = 1;
        }
        mask = mask >> 1;
    }

    if(flag == 1)
    {
      printf(" bits are set\n");
    }
    else
    {
       printf("\nNone of the bits are set\n");
    }
}

void set_unset(int *inp,int op)
{
    int n,temp;
    unsigned int mask;

    if(op == S)
    {
        printf("\nEnter the bit(1-32) which you want to set: ");
        scanf("%d",&n);

        mask = 0x80000000;

        mask = mask>>(n-1);

        temp = *inp;
        if((*inp = *inp | mask ) == temp)
        {
            printf("\nThe bit was set already\n");
            return;
        }
    }
    else if(op == U)
    {
        printf("\nEnter the bit(1-32) which you want to unset: ");
        scanf("%d",&n);

        mask = 0x80000000;

        mask = mask>>(n-1);

        temp = *inp;
        if((*inp = *inp ^ mask ) == temp)
        {
            printf("\nThe bit was unset already\n");
            return;
        }

    }
    else
    {
        printf("\nSecond Argument must be S or U !!!");
        return;
    }
}

void check_set(int inp)
{
    int n;
    printf("\nEnter which bit you want to check : \n");
    scanf("%d",&n);

    unsigned int mask = 0x80000000 >> n-1; //Mask is 32bit 1000 0000 0000 0000 0000 0000 0000 0000
    if ( mask & inp )
    {
        // AND (&) of the two will be 0000 0000 0000 0000 (false)
        printf("Input has %dth bit set. \n",n);
    }
    else
    {
        printf("Input has %dth bit unset. \n",n);
    }

}

void showbits(unsigned int value)
{
    unsigned int bit;
    for ( bit = (-1U >> 1) + 1; bit > 0; bit >>= 1 )
    {
        putchar(value & bit ? '1' : '0');
    }
    putchar('\n');
}

void reverse_bits(unsigned int *inp)
{
    unsigned int rev = *inp;
    int s = sizeof(*inp) * CHAR_BIT - 1; // extra shift needed at end

    for (*inp >>= 1; *inp; *inp >>= 1)
    {
        rev = rev << 1;
        rev = rev | (*inp & 1);
        s--;
    }
    rev = rev << s;
    *inp = rev;
    showbits(*inp);
}

int main()
{
    int input ,ch ;

    printf("\nEnter an integer of size %d bytes to be displayed in binary : ",sizeof(int));
    scanf("%d", &input);

    while(1)
    {
        printf("\n1.Toggle all bits\n2.Detect all set bits\n3.Set a bit\n4.Unset a bit\n5.Check bit status\n6.Reverse bit pattern\n7.Show the bit pattern\n8.Exit\nEnter your choice : ");
        scanf("%d",&ch);

        switch(ch)
        {
        case 1 :
            toggle(&input);
            break;
        case 2 :
            detectset(input);
            break;
        case 3 :
            set_unset(&input,S);
            break;
        case 4 :
            set_unset(&input,U);
            break;
        case 5 :
            check_set(input);
            break;
        case 6 :
            reverse_bits(&input);
            break;
        case 7 :
            showbits(input);
            break;
        case 8 :
            exit(0);
        }
    }
}

Comments