logo CodeStepByStep logo

bitset1

Author: Julie Zelenski (on 2018/02/03)

Bit masks are an important part of coding bitwise operations. Often you might need to set or clear a particular bit in a mask, and the following functions accomplish that:

int bitset(int num, int pos)
{
    return num | (1 << pos);
}

int bitclear(int num, int pos)
{
    return num & (~(1 << pos));
}

Given the above functions, what is the return value in base-10 for each of the following expressions?

bitset(22, 5)
bitset(15, 31)
bitset(12, 0)
bitclear(54, 5)
bitclear(15, 31)
bitclear(-12, 31)

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.