Tuesday, 13 February 2018

MACRO for bitwise operation's


/* MACRO for bitwise operation's */

# ifndef __BITWISE__
# define __BITWISE__

/* Assign the Maximum value of the integer */

# define MAX (-1) >> 1 

/* Mask the odd bit's in the given number */

# define O_MASK   0xAAAAAAAA

/* Mask the even bit's of the given number */

# define E_MASK   0x55555555

/* Find the number is power of four or not */

# define P_FOUR(num)  (num & O_MASK) ? (!(num & (~(-num)))) ? 1 : 0 : 0 

/* Find the number is power of two or not */

# define P_TWO(num)  (num) ? (num & (~(-num))) ? 0 : 1 : 0

/* Find the given number is multiply by 2,4,8 or not */

# define MUL_2(num) (num & 1) ? 0 : 1

# define MUL_4(num) (num & 3) ? 0 : 1

# define MUL_8(num) (num & 7) ? 0 : 1 

/* Find the given number is multiply by 9 or not */

# define MUL_9(num) ((num << 3) | (num))

/* Arithmetic addition of two number's using bitwise operation */

# define ADD(a,b)   while(b = ((((a ^= b) ^ b) & b ) << 1 )) ;

/* Arithmetic subtraction of two number's using bitwise operation */

# define SUB(a,b)    while(b = ((~((a ^= b) ^ b) & b ) << 1 )) ;

/* Set the particular bit in the given number */

# define SET(num,pos)  num = num | (1 << pos)

/* Clear the particular bit in the given number */

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

/* Set two bit's in the given number using two different position's */

# define SET_2_BIT(num,p1,p2)  num = ((num | (1 << p1)) | (num | (1 << p2)))

/* Complement the bit's in a given number for a particular source and destination position */

# define COMP(num,src,des) num = num ^ ( (unsigned) -1  >> ((32 - (des-src))-1) ) << src

/* Complement the even bit's in the number */

# define C_EVEN(num)  num = num ^ E_MASK 

/* Complement the odd bit's in the number */

# define C_ODD(num)  num = num ^ O_MASK

/* Toggle the entire bit's in the number */

# define TOG(num)  num = ~num

/* Find the greatest number from the given two number's */

# define SWP_NIB_CHR(n)   n = (n << 4) | (n >> 4)
# define GREAT(num1,num2) ((num1 - num2) >> 31) ? num2 : num1 

/* Increment operator using bitwise */

# define INC(num)  num = -(~num)

/* Decrement operator using bitwise */

# define DEC(num)  num = ~(-num)

/* Check the MSB bit is set or not */

# define HIGHT(num)  (num & (1 << (sizeof(num)*8)-1)) ? 1 : 0

/* Check the LSB bit is set or not */

# define LOW(num)  (num & 1) ? 1 : 0 

/* Right Rotate the given number by n times */

# define R_RIGHT(num,n)  num = ((num >> n) | (num << 32-n))

/* Left Rotate the given number by n times */

# define R_LEFT(num,n)   num = ((num << n) | (num >> 32-n))

/* Swap the two integer number's using bitwise */

# define SWAP_NUM(a,b)  a = a ^ b; b = a ^ b;  a = a ^ b; 

/* Swap the adjacent bit's in the given number */

# define SWAP_ADJ(num) num = (((num & E_MASK) >> 1) | ((num & O_MASK) << 1));

/* Swap the nibble's from the given integer number */

# define INT(num) num = ((num << 28) | (num >> 28) | (num & 0x0ffffff0))
# define INT_S(num) num = ( (num & 0x0A0A0A0A ) >> 4 ) | ( ( num & 0xA0A0A0A0) << 4 )

/* Swap the nibble's from the given charecter */

# define SWP_NIB_CHR(n)   n = (n << 4) | (n >> 4)

/* convert the charecter from upper to lower case   */

# define TO_LOWER(c) (c >= 65 && c <= 91) ? c |= 1 << 5 : 0

/* convert the charecter from lower to upper case   */

# define TO_UPPER(c) (c >= 97 && c <= 122) ? c ^= 1 << 5 : 0

/* count trailing zeros in the given number */

static inline int trailing(unsigned int num)
{
 int count = 0;
 for(;((!(num & 1)) && count!= 32 );num >>= 1,count++) ;
 return count;
}

/* count the number of ones in an given number */ 

static inline int count_set(unsigned int num)
{
 int count = 0;
 for(; num ;count += num & 1, num >>= 1) ;
 return count;
} 

/* print the next largest number containing same number of 1's and 0's  */
 
static inline int next_large(unsigned int num)
{
 int f,s = 0;
 for(f = count_set(num) ; f != s ; s = count_set(INC(num))) ;
 return num;
}

/* check whether given number is bit pallindrome or not  */

static inline int palindrome(unsigned int num)
{
 int i;

 for(i = 0;(((num >> i) & 1 ) == ((num >> 31-i) & 1)) && i < 16; i++) ;
 
 (i == 16)? 1 : 0;
}
/* Display the binary form of the given number */

void show(int num) 
{
 int i; 
 for(i = 31;i>=0;i--) 
  (num & (1 << i)) ? printf("1 ") : printf("0 ");
}

# endif

/* End of the file */

/* fun  swap the bits in an integer
 *
 * @n,@d,@s
 *n : number
  d : destination bit position in number 'n'
  s : source bit position in number 'n'

 * return an integer obtained after swapping the bits
 *
 */

int bit_swap(int n , int d , int s)
{
 return (((n >> d) ^ (n >> s )) & 1 ) ? (n ^ ( (1 << d) |
           (1 << s) )) : n ;
}

/*  fun  copy a bit from a number from specified postion to specified position in the other number
 *
 *  @snum,@s,@dnum,@d
 *
 *  return  integer 
 */

int bit_cp(int s , int d , int snum , int dnum)
{
 
 return (snum & (1 << s )) ? ( dnum | ( 1 << d ) ) : (dnum & ~(1 << d ));
}

/* complements the even bits in an integer */


int even_bit_toggle(int num)
{
 int tmp = 1;

 while( tmp ) {

  num ^= tmp;
  tmp <<= 2;
 }

 return num;
}

/* fun  to_lower converts the input character ch into lower case if it is upper case
 *
 * @ch
 *
 * return lower case of the i/p if it was upper case , else returns as it is
 */

int to_lower(int ch)
{
 return  (ch >= 65 && ch <= 90) ?  ch  | ( 1 << 5 )  : ch ;
}

/* fun converts the input character into upper case ,if it was lower case
 *
 *@ch

 return  upper case character if the input is lower case else return as it is

 */
int to_upper(int ch)
{
 return  (ch >= 97 && ch <= 122) ?  ch & ~( 1 << 5)  : ch ;
}

/* fun add , performs bitwise addition on two integers
 *
 * @num1,@num2
 *
 * return sum of the two integers num1 and num2
 *
 */

int add(int num1 , int num2)
{
 int tmp = 0;

 while( num2 ) {
  tmp = num1 ^ num2;
  num2 = (num1 & num2) << 1;  /*left shifting carry by 1 */
  num1 = tmp;
 }

 return num1;

 
}

/* fun add , performs bitwise subtraction on two integers
 *
 * @num1,@num2
 *
 * return subtraction of the two integers num1 and num2
 *
 */

int sub(int num1 , int num2)
{
 int tmp = 0;

 while( num2 ) {
  tmp = num1 ^ num2;
  num1 = tmp;
  num2 = (num1 & num2) << 1;  /*right shifting carry by 1 */

 }

 return num1;

 
}

/* fun mul , performs bitwise multiplication on two integers
 *
 *@num1,@num2

 returns product of the integers num1 and num2

*/

int mul(int num1 , int num2)
{
 int sign1 = 1 , sign2 = 1 ;
 if ( num1 < 0 )
  sign1 = -1;  /* storing the sign  */
 if(num2 < 0)
  sign2 = -1;

 int sum = 0;  /* to store the product of num1 and num2  */

 for( num1 *= sign1 , num2 *= sign2; num2 ; num1 <<= 1 , num2 >>= 1)
  if ( num2 & 1)
   sum += num1;

 return (sum * sign1 * sign2 );
}

/* fun div , performs bitwise division through repeated subtraction
 *
 *@num1,@num2

 returns quotient after the division

*/

int sub(int num1 , int num2);
int _div(int num1 , int num2)
{
 int sign1 = 1 , sign2 = 1, count = 0;
 if ( num1 < 0 )
  sign1 = -1;
 if(num2 < 0)
  sign2 = -1;

 num1 *= sign1;   /* will turn num1 and num2 positive ,if it was negative */
        num2 *= sign2;
 while((num1 >= num2)) {
  num1 = sub(num1,num2);
  ++count;
 }

 return (count * sign1 * sign2 );
}

/* fun rotate the bits in a number in either direction
 *
 *
 * @num ,@nbit ,@dir
 * 
 *dir == 0 : left rotation  dir == 1 , right rotation
 * return obtained integer after the rotation
 *
 */

int size();
int rotate_bit( int num , int nbit , int dir)
{
 //return dir ? ((num >> nbit) | ( num << (size() - nbit))) :(( num << nbit) | ( num >> (size() - nbit) ));
 return dir ? ((num >> nbit) | ( num << (size() - nbit))) :(( num << nbit) | ( num >> (size() - nbit) ));
}


/* fun  size calculates size of a compiler , using integer, var is assigned with ~0
 *  i.e all bits are set , counting each set bit with every right shift untill it reaches to 0
 *
 * @void
 *
 * return the size of a compiler
 *
 */

int size()
{
 int s = 1;
 unsigned var = (unsigned) ~0;

 for ( ; (var >>= 1 ) ; ++s)
 ;
 return s;
}














No comments:

Post a Comment