bitwise question

You are given two 32-bit numbers, N and M, and two bit positions, i and j. Wirte a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, beacause M could not fully fit between bit 3 and bit 2.
EXAMPLE
Input: N = 100 0000 0000, M = 10011, i =2, j = 6
Output: N = 10001001100″ [1]

Solution
Hint
First, create a number with all ones. We call this number “allOnes”. So allOnes = 1111 1111 1111 1111 1111 1111 1111 1111.
Second, shift the number left by j+1 bits. We call this number “left”. So left = 1111 1111 1111 1111 1111 1111 1000 0000.
Third, shift number 1 left by i bits. The number should be 0000 0100.
Fourth, use this number to minus 1. We call the result “right”. So right = 0000 0100 – 0000 0001 = 0000 0011.
Fifth, calculate left OR right. We call this result as “mask”. So mask = left | right = 1111 1111 1111 1111 1111 1111 1000 0011.
Sixth, M = M & mask. So M = 100 0000 0000.
Seventh, shift N left by i bits. So N = N << i = 100 1100.
The final result = M | N = 100 0000 0000 | 100 1100 = 100 0100 1100.
Code

int insertNum(int m, int n, int i, int j)
{
    if(i >= j)
    {
        cout << "Cannot insert number because i > j" << endl;
        return m;
    }
    int allOnes = ~0;
    int left = allOnes << (j+1);
    int right = (1 << i) - 1;
    int mask = left | right;
    m = m & mask;
    n = n << i;
    return m | n;
}

Add and substract two numbers without using arithmetic operators

Add two numbers without using arithmetic operators

int Add(int x, int y)
{
    // Iterate till there is no carry  
    while (y != 0)
    {
        // carry now contains common set bits of x and y
        int carry = x & y;  
 
        // Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y; 
 
        // Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1;
    }
    return x;
}

Subtract two numbers without using arithmetic operators

int subtract(int x, int y)
{
    // Iterate till there is no carry
    while (y != 0)
    {
        // borrow contains common set bits of y and unset bits of x
        int borrow = (~x) & y;
 
        // Subtraction of bits of x and y where at least
        // one of the bits is not set
        x = x ^ y;
 
        // Borrow is shifted by one so that subtracting it from
        // x gives the required sum
        y = borrow << 1;
    }
    return x;
}