Showing posts with label Bit manipulation. Show all posts
Showing posts with label Bit manipulation. Show all posts

Check if two numbers are equal without any comparison

Logic 1: 

If two numbers are equal the bitwise XOR result will be zero. In this way we can check two numbers are equal without any comparison.

For example :

1) 5^5 = 0

(5)10 = (0101)2
0101
0101
-------
0000

2) 12^12 = 0
(12)10 = (1100)2
1100
1100
------
0000

C program :
int main()
{
    int num1 = 10, num2 = 10;
    if((num1 ^ num2) == 0)
        printf("Equal");
    else
        printf("Unequal");
    return 0;
}

Logic 2 : 

We know, subtraction of two equal numbers is Zero. Means number are zero if equal other wise non-zero.
C program :
int main()
{
    int num1 = 10, num2 = 10;
    if(!(num1 - num2))
        printf("Equal");
    else
        printf("Unequal");
    return 0;
}

Set all the bits in given range of unsigned int

First discuss about what is bits setting? 
  1. Number 12 is represented as 1100 in binary format. Mathematical representation like (12)10 = (1100)2So Number (1100)2 then complete bits setting result is (1111)2, means set all bit to 1, this is called complete/all bits setting.
  2. But in some cases we need to set particular bit of Number. For example (1100)2 set 1st bit only, then result become (1110)2, (bit counted from right to left, counting started with number 0), this is single bit setting.
  3. Now we can start with set bits of number with given range. For example (1100)2 set bits from 1st to 3rd bit, then result become (1110)2. This is given rang bits setting.
Now we can start with how to code this in C.

All bits setting:

Suppose N is unsigned integer value.
 Then we can code in C like this:
unsigned int all_bits_setting(unsigned int N)
{
  return N | ~0
}


Single bit setting:

Single bit setting is possible with following method: 
Input is (1100)2  and 2nd bit need to set, then first create binary number which contain 1 as a bit value at 1nd bit. Suppose number is X, that we can create X like this 1 << 2 = (0010)2, do OR operation of both numbers (1100)2  | (0010)2 = (1110)2

Then we can code in C like this:

unsigned int single_bit_setting(unsigned int N, unsigned int k)
{
  return (N | (1 << k)); 
}

Set given range of bits:

Input is (1100)2  and range is from 1st bit to 3rd bit. Then first create binary number which contain 1 as bit value from 1st bit to 3rd bit and other bits values are 0. So required value is (1110)2. But how to created (1110)using given rang ? So first find range difference i.e. 3 -1 = 2, then (1 <<  (2+1)) -1 = (7)10 = (0111)2 , Now shift this value with lowest rang value  (0111)2  << 1 = (1110)2, Now do OR operation of Input value  (1101)2 and (1110)2 ,
(1100)2 | (1110)=  (1110)2

Then we can code in C like this:
unsigned int rang_toggling(unsigned int N, unsigned int k , unsigned int l)
{
 //Here l > k
  return N | ((1<<(l-k)+1)-1)<<k;
}

Clear all the bits in given range of unsigned int

First discuss about what is bits clearing? 
  1. Number 12 is represented as 1100 in binary format. Mathematical representation like (12)10 = (1100)2So Number (1100)2 then complete bits clearing result is (0000)2, means set all bit to 0, this is called complete/all bits clearing.
  2. But in some cases we need to clear particular bit of Number. For example (1100)2 clear 2st bit only, then result become (1000)2, (bit counted from right to left, counting started with number 0), this is single bit clearing.
  3. Now we can start with clear bits of number with given range. For example (1101)2 clear bits from 1st to 3rd bit, then result become (0001)2. This is given rang bits clearing.
Now we can start with how to code this in C.

All bits clearing:

Suppose N is unsigned integer value.
 Then we can code in C like this:
unsigned int all_bits_clearing(unsigned int N)
{
  return N & 0
}


Single bit clearing:

Single bit clearing is possible with following method: 
Input is (1100)2  and 2nd bit need to clear, then first create binary number which contain 1 as a bit value at 2nd bit. Suppose number is X, that we can create X like this 1 << 2 = (0100)2, Now do bitwise complement of (0100)2, so binary after bitwise complement is (1011)2, do AND operation of both numbers (1100)2  & (1011)2 = (1000)2

Then we can code in C like this:

unsigned int single_bit_clearing(unsigned int N, unsigned int k)
{
  return (N & (~(1 << k))); 
}

Clear a given range of bits:

Input is (1101)2  and range is from 1st bit to 3rd bit. Then first create binary number which contain 0 as bit value from 1st bit to 3rd bit and other bits values are 1. So required value is (0001)2. But how to created (0001)using given rang ? So first find range difference i.e. 3 -1 = 2, then (1 <<  (2+1)) -1 = (7)10 = (0111)2 , Now shift this value with lowest rang value  (0111)2  << 1 = (1110)2, Now do bitwise complement of (1110)2 so binary value after bitwise complement is (0001)2 ,  Now do AND operation of Input value  (1101)2 and (0001)2 ,
(1100)& (0001)=  (0001)2

Then we can code in C like this:
unsigned int rang_toggling(unsigned int N, unsigned int k , unsigned int l)
{
 //Here l > k
  return N & ~((1<<(l-k)+1)-1)<<k;
}

Toggle a given range of bits of an unsigned int

First discuss about what is bits toggling? 
  1. Number 12 is represented as 1100 in binary format. Mathematical representation like (12)10 = (1100)2So Number (1100)then complete toggled result is (0011)2, Here 1 is replaced with 0 and 0 with 1, this is called bitwise complement.
  2. But in some cases we need to toggle particular bit of Number. For example (1100)2 toggle 1st bit only, then result become (1110)2, (bit counted from right to left, counting started with number 0), this is single bit toggling.
  3. Now we can start with toggle bits of number with given range. For example (1100)2 toggle bits from 1st to 3rd bit, then result become (0010)2. This is rang bits toggling.
Now we can start with how to code this in C.

Bitwise complement: 

Suppose N is unsigned integer value.
 Then we can code in C like this:
unsigned int bitwise_complement(unsigned int N)
{
  return ~ N
}
Operator ~ is used to get bitwise complement.


Single bit toggling:

Single bit toggle is possible with following method: 
Input is (1100)2  and 2nd bit need to toggle, then first create binary number which contain 1 as bit value at 2nd bit. Suppose number is X, that we can create X like this 1 << 2 = (0100)2, Now do XOR of both number (1100)2 ^ (0100)2 = (1000)2

Then we can code in C like this:

unsigned int single_bit_toggling(unsigned int N, unsigned int k)
{
  return (N ^ (1 << k)); 
}

Toggle a given range of bits:

Input is (1100)2  and range is from 1st bit to 3rd bit. Then first create binary number which contain 1 as a bit value from 1st bit to 3rd bit and other bits values are 0. So required value is (1110)2. But how to created (1110)2 using given rang ? So first find range difference i.e. 3 -1 = 2, then (1 <<  (2+1)) -1 = (7)10 = (0111)2 , Now shift this value with lowest rang value  (0111)<< 1 = (1110)2, This is required value to do XOR with input
(1100)2 ^ (1110)2 =  (0010)2

Then we can code in C like this:
unsigned int rang_toggling(unsigned int N, unsigned int k , unsigned int l)
{
 //Here l > k
  return N ^ ((1<<(l-k)+1)-1)<<k;
}