Tuesday 9 August 2016

Anirudh

Some bit manipulation puzzles in C

Most programming languages like C, C++, Java, Python offer various bit level manipulation operators (bitwise operators) and many puzzling questions can be formed based upon these operators. I am sharing some of such bit manipulation questions here. Attempting challenging programs like these is a great way to learn about the bit level representation of numbers and the various bitwise operators (&, |, ^, ~, >>, <<).

These questions require you to implement a piece of code with only a limited set of operators.

Write a function to perform bitwise AND using ~ and | operators only.
int bitAnd(int a, int b)
{
    return ~(~a | ~b);
}

Write a function to perform bitwise XOR using ~ and & operators only.
int bitXor(int a, int b)
{
    return ~(~(~a & b) & ~(a & ~b));
}

Write a function to give sign info about a number x without using conditional statements, >, <, == and - operators. Return 1 if x > 0, -1 if x < 0 and 0 if x = 0.
int sign(int x)
{
    return (x>>31) | (!!x);
}

Write a function to compare 2 numbers without using conditional statements, >, <, == and - operators. Return 1 if a > b, -1 if a < b and 0 if a = b.
Here, negative of b is obtained by taking its 2's complement using biwsise operators. This is then added to a and the sign of the result is determined. See above question for sign() function.
int compare(int a, int b)
{
    return sign(a + (~b + 1));
}

Write a function to return logical not of a given number x (i.e. !x), without using ! operator and conditional statements.
int not(int x)
{
    int neg_x = ~x + 1;
    return 1 ^ ((x>>31 & 1) | (neg_x>>31 & 1));
}
Another way: The second way exploits the fact that the right hand side of a logical AND (&&) is never evaluated if the left hand side is false (0) and similarly, the right hand side of a logical OR (||) is never evaluated if the left hand side is true (non-zero).
int not(int x)
{
    int res;
    (x && ~(res=0)) || (res=1);
    return res;
}

Write a function to implement conditional operator ?: using bitwise operators only.
int conditional(int a, int b, int c)
{
    return ((~(!a<<31) >> 31) & b) | (((!a<<31) >> 31) & c);
}

/*
    Another approach could have been like this:
    int res;
    (a && (res=b)) || (res=c);  // But this WILL NOT WORK in case a=1 and b=0 
                                // (as "res = b" returns b, so it will return 0 on the LHS of ||
                                // and then res will then be set to c).
    return res;
*/


Write a function to print binary representation of a given number.
void printBinary(int x)
{            
    int i;
    int msb = (sizeof(int) * 8) - 1;  // 31 for a 4 byte int

    for(i = msb; i >= 0; i--)
        printf("%d", (x>>i) & 1);
}

Anirudh

About the author →

Anirudh Khanna is a Computer Science student and a geek who firmly believes in the awesomeness of technology! He is a programmer and web designer, also keenly interested in the research of new and innovative ideas in Computer Science.

Subscribe to Geek Factorial via email :

3 comments

Write comments
Anonymous
AUTHOR
August 09, 2016 2:19 pm delete

Loving the content. Keep posting bro. Very useful.

Reply
avatar
Anirudh
AUTHOR
August 09, 2016 6:24 pm delete

I'm glad you found it interesting. Thanks.

Reply
avatar
October 12, 2018 3:10 pm delete


I like your blog, I read this blog please update more content on python, further check it once at python online course

Reply
avatar