Header Ads

19 - WAP to Set, Get, Toggle, & Print 'n' Bits from given Number in C

This Program performs following Operations :

1) Get 'n' Bits

2) Set 'n' Bits

3) Get 'n' Bits from position

4) Set 'n' Bits from position 

5) Toggle 'n' Bits from position

6) Print Bit's

#include 

//Get n_bits from lsb
int get_nbits (int num, int bits)
{
 int ans, mask;
 //Initializing Mask
 mask = (1 << bits) - 1;
 //Getting the bits
 ans = num & mask;
 return ans;
}

//Set n_bits of number from value
int set_nbits (int num, int val, int bits)
{
 int ans, mask;
 //Initializing mask
 mask = (1 << bits) - 1;
 //Getting the bits from value
 val &= mask;
 //Replacing it on the number
 num = (num & ~mask) | val;
 return num;
}

//Get n_bits from position
int get_nbits_pos (int num, int bits, int pos)
{
 int ans, mask;
 //Initializing mask and position
 mask = (1 << bits) - 1;
 pos = pos - bits + 1;
 //Getting the bits from position
 ans = num & (mask << pos);
 ans = ans >> pos;
 return ans;
}

//Set n_bits from positon from value
int set_nbits_pos (int num, int val, int bits, int pos)
{
 int get, result;
 get = pos - bits + 1;
 val =(val & ((1 << bits)-1)) << get;
 num = num & ~(((1 << bits) -1) << get);
 result = num | val;
 return result;
}

//Toggle bits
int toggle_bits (int num, int bits, int pos)
{
 int get, temp, result; 
 temp = num;
 get = pos - bits + 1;
 num = (~num & ((1 << bits) - 1) << get);
 temp = temp & ~(((1 << bits) - 1) << get);
 result = num | temp;
 return result;
}

//Print bits
int print_bits (int num, int bits)
{
 int idx;
 printf("The %d bits of the number %d are ", bits, num);

 //Loop to print the bits
 for (idx = bits - 1; idx >= 0; idx--)
 {
  //Condition to print 1's
  if ( num & (1 << idx) )
  {
   putchar('1');
  }
  else
  {
   putchar('0');
  }

 } 
 printf("\nThe 2's Complement of the number is : ");

 //2's Complement of the number
 num = ~num + 1;

 //Loop to print the bits
 for (idx = bits - 1; idx >= 0; idx--)
 {
  //Condition to print 1's
  if ( num & (1 << idx) )
  {
   putchar('1');
  }
  else
  {
   putchar('0');
  }
 } 
}

int main ()
{
 int choice, num, bits, ans, val, pos;
 char ch;

 while(1)
 {
  printf("Choose any function to perform the following operations\n1.Get n bits\n2.Set n bits\n3.Get n bits from position\n4.Set n bits from position\n5.Toggle n bits from position\n6.Print bits\n");

  scanf("%d", &choice);

  //Switch case to select the choice
  switch (choice)
  {
   //get_nbits
   case 1: printf("Enter the Number : ");
    //Reading number
    scanf("%d", &num);
    printf("Enter the number of Bits : ");
    //Reading no.of bits
    scanf("%d", &bits);
    //function call
    ans = get_nbits(num, bits);
    printf("The %d bits from the number %d is %d \n",bits, num, ans);
    break;

    //set_nbits
   case 2: printf("Enter the Number and Value : ");
    //Reading number and value
    scanf("%d %d", &num, &val);
    printf("Enter the number of bits : ");
    //Reading no.of bits
    scanf("%d", &bits);
    //function call
    ans = set_nbits(num, val, bits);
    printf("The number after replacing %d bits from the Value %d is %d\n",bits, val, ans);
    break;

    //get_nbits_pos
   case 3: printf("Enter the Number : ");
    //Reading number
    scanf("%d", &num);
    printf("Enter the number of bits and position : ");
    //Reading no.of bits and position
    scanf("%d %d", &bits, &pos);
    //function call
    ans = get_nbits_pos(num, bits, pos);
    printf("The %d bits from the position %d of the number %d is %x\n",bits, pos, num, ans); 
    break;

    //set_nbits_position
   case 4: printf("Enter the Number and Value : ");
    //Reading number and value
    scanf("%d %d",&num, &val);
    printf("Enter the number of bits and pos : ");
    //Reading its and position
    scanf("%d %d",&bits, &pos);
    ans = set_nbits_pos(num, val, bits, pos);
    printf("The number after replacing %d bits from the position %d of the Value %d is %d\n",bits, pos, val, ans);
    break;

    //Toggle bits
   case 5: printf("Enter the Number : ");
    //Reading number
    scanf("%d",&num);
    printf("Enter the number of bits and pos : ");
    //Reading bits and position
    scanf("%d %d",&bits, &pos);
    ans = toggle_bits(num, bits, pos);
    printf("The number %d after toggling %d from the position %d is %d\n", num, bits, pos, ans);
    break;

   case 6: printf("Enter the Number : ");
    //Reading the number from user
    scanf("%d", &num);
    printf("Enter the number of bits : ");
    //Reading the no. of bits 
    scanf("%d", &bits);
    //Checking if no.of bits are greater than the size of integer
    if (bits > 32)
    {
     bits = 32;
    }
    ans = print_bits(num, bits);
    //printf("The %d bits from the number %d are %d\n",bits, num, ans);
    break;
  }    
  printf("\nTo continue press 'Y' ");
  getchar();
  ch = getchar();
  if (!(ch == 'Y' | ch == 'y'))
  {
   break;
  }
 }
 return 0;
}

No comments