logo CodeStepByStep logo

bittoggle

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));
}

Write a function named bittoggle that accepts two int arguments num and pos, and toggles the bit at the pos'th position of num, returning the result. For example, the call of bittoggle(22, 5) should return 54.

Function: Write a C function as described, not a complete program.

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.