C bitwise operator interview questions

1) Write a C program to set and reset a bit using bitwise operators.
#include<stdio.h>
int main()
{
        int number, bit;
        printf("Enter a number\n");
        scanf("%d", &number);

        //set
        printf("Enter a bit to set\n");
        scanf("%d", &bit);
        number = number | (1 << bit);
        printf("After setting a bit %d, number is %d\n",bit, number);

        //reset
        printf("Enter a bit to reset\n");
        scanf("%d", &bit);
        number = number & ~(1 << bit);
        printf("After re-setting a bit %d, number is %d\n",bit, number);
}

o/p:
Enter a number
10
Enter a bit to set
2
After setting a bit 2, number is 14
Enter a bit to reset
3
After re-setting a bit 3, number is 6

Explanation: 
OR operation of '0' or '1'  with '1' becomes '1'
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 1
number = number | (1 << bit);
using this syntax, moving 1 to given bit position and the OR operation will set the bit.

AND operation of '0' or '1'  with '0' becomes '0'
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

let's say number is 14 and bit 2 need to reset.
binary of 14 is '0(b7) 0(b6) 0(b5) 0(b4) 1(b3) 1(b2) 1(b1) 0(b0)'
b2 is bit number 2 need to reset.

number = number & ~(1 << bit);

binary of '1' is 0000 0001
(1 << bit) will move '1' to bit number position '2'. value of (1 << bit) =  0000 0100
~(1 << bit) = 1111 1011

number & ~(1 << bit); =  0000 1110 & 
                                           1111    1011
                                       -----------------
                                           0000 1010 = 6

Basic ideal is take '1' to the given position. then set '1' to '0' and other all bit to '1'. So, that AND operation will set given bit to '0' and other all bits are unaffected in a number.
2) Write a C program to toggle a bit using bitwise operators.
#include<stdio.h>

int main()
{
        int number, bit;
        printf("Enter a number and bit to toggle\n");
        scanf("%d %d", &number, &bit);
        number = number ^ (1 << bit);
        printf("Number after toggling a bit %d is %d\n", bit, number);
}
o/p:
Enter a number and bit to toggle
10
1
Number after toggling a bit 1 is 8

Explanation:

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

In EX-OR operation, if both the bits are same then result is '0'
if both the bits are different then result is '1'

number = number ^ (1 << bit);
So, take 1 to the given position.
then if given position is '1' in number then 1 ^ 1 = 0, and if given position is '0' in the number then 0 ^ 1 = 1.

let's say number is 10 and bit is 1.
binary of 10 is 0000 1010
1 << bit = 1 << 1 = 0000 0010

0000 1010 ^
0000 0010
-----------
0000 1000 = 8
3) Write a program to reverse the bits of a given number using bitwise operators.
//Problem explanation: if number is 10. 10's binary is 1010, reverse will be 0101 which is '5' 

#include<stdio.h>
#include<stdint.h>

int main()
{
        uint32_t number;
        printf("enter a number to reverse\n");
        scanf("%x", &number);

        for (int i = 0, j = 31; i < j  ; i++, j--)
        {
                if (((number >> i) & 1) != ((number >> j) & 1))
                {
                        number ^= (1 << i);
                        number ^= (1 << j);
                }

        }
        printf("After a reversing  number is %x\n", number);

}

o/p:
enter a number to reverse
0x12345678
After a reversing  number is 1e6a2c48

Explanation:
here, number is taken as 32 bit size. 
entering number in hex and printing in hex.
0x12345678 = 00010010001101000101011001111000
0x1e6a2c48 = 00011110011010100010110001001000

Basically, idea is to traverse from first and last bit untill it reaches to middle, if both are euqal then no need to do anything. if not equal then just toggle.
4) Write a program to test a bit in a number.
//Problem explanation: check if bit is set to '1' or '0' in given number. 
 
#include<stdio.h>

int main()
{
        int number, bit;

        puts("Enter a number and bit to test");
        scanf("%d %d", &number, &bit);

        if ((number >> bit) & 1)
                printf("bit %d is set\n", bit);
        else
                printf("bit %d is not set\n", bit);
}

o/p:
Enter a number and bit to test
10 1
bit 1 is set

Explanation:
number is 10, binary is 0(b7) 0(b6) 0(b5) 0(b4) 1(b3) 0(b2) 1(b1) 0(b0)
bit-1(b1) is set.

->(number >> bit) & 1)
Idea is right shift number by bit postition. 0000 1010 >> 1 = 0000 0101
then AND with 1 will identify '0' or '1' at given position, 0000 0101 & 0000 0001 = 1
5) Write a program to find whether the given number is odd or even.
#include<stdio.h>

int main()
{
        int number;
        printf("Enbter a number\n");
        scanf("%d", &number);

        (number & 1) ? puts("number is odd") : puts("number is even");
}

o/p:
Enbter a number
3
number is odd

Explanation:
odd number always have first bit '1' in the binary of number.

if numbe is 3. Binary is 0000 0011, first bit is 1. 
(number & 1) = 0000 0011 & 0000 0001 = true(1) = number is odd
if number is 4. Binary is 0000 0100, first bit is 0.
(number & 1) = 0000 0100 & 0000 0001 = false(0) = number is even
6) Write a program to swap two variables values using the bitwise XOR operator without using any additional(temporary) variable.
#include<stdio.h>

int main()
{
        int x = 10, y = 12;
        x = x^y;
        y = x^y;
        x = x^y;
        printf("number after swap x=%d y=%d\n", x,y);
}

o/p:
number after swap x=12 y=10

Explanation:
x = 10 = 1010
y = 12 = 1100

x = x^y = 10^12 = 1010 ^ 1100 = 0110
So, x = 0110

y = x^y = 0110^1100 = 1010 = 10
So, y = 1010 = 10

x = x^y = 0110^1010 = 1100 = 12
So, x = 1100 = 12
7) Write a program to multiply an integer number with a power of 2 and divide an integer number by a power of 2.
#include<stdio.h>

int main()
{
        int num, multiplier, devider;
        printf("Enter a number and multiplier of power of 2\n");
        scanf("%u %u", &num, &multiplier);

        while(multiplier >= 2)
        {
                num = num << 1; 
                multiplier = multiplier >> 1;
        }

        printf("number is %d after multiplied by %d\n", num, multiplier);

        printf("Enter a number and devider of power of 2\n");
        scanf("%u %u", &num, &devider);

        while(devider >= 2)
        {
                num = num >> 1;
                devider = devider >> 1;
        }

        printf("number is %d after devided by %d\n", num, devider);

}

o/p:
Enter a number and multiplier of power of 2
10
32
number is 320 after multiplied by 1
Enter a number and devider of power of 2
32
8
number is 4 after devided by 1

Explanation:
any integer value left shifted by 1 is equal to multipled by 2.
any integer value right shifted by 1 is equal to devided by 2.

num = num << 1; 
multiplier = multiplier >> 1;
here, left shifting means multiplying num by 2 until multiplier becomes less than 2.
10's binary is 1010 
In 1st iteration
num = 1010(10) << 1 = 10100(20), multiplier = 100000(32) >> 2 = 10000(16)
In 2nd iteration 
num = 10100(20) << 1 = 101000(40), multiplier = 10000(16) >> 1 = 1000(8)
In 3rd iteration 
num = 101000(40) << 1 = 1010000(80), multiplier = 1000(8) >> 1 = 100(4)
In 4th iteration 
num = 1010000(80) << 1 = 10100000(160), multiplier = 100(4) >> 1 = 10(2)
In 5th iteration
num = 10100000(80) << 1 = 101000000(320), multiplier = 10(2) >> 1 = 1(1)
then it will come out of the while loop.

num = num >> 1;
devider = devider >> 1;
here, right shifting means deviding num by 2 until devider becomes less than 2.
32's binary is 100000 
In 1st iteration
num =  100000(32) >> 1 = 10000(16), devider = 1000(8) >> 1 = 100(4)
In 2nd iteration
num =  10000(16) >> 1 = 1000(8), devider = 100(4) >> 1 = 10(2)
In 3rd iteration
num =  1000(8) >> 1 = 100(4), devider = 10(2) >> 1 = 1(1)
then it will come out of the while loop.
8) Print a binary of unsigned 32-bit integer number.
#include<stdio.h>
#include<stdint.h>

int main()
{
        uint32_t num;
        printf("Enter a number\n");
        scanf("%d", &num);

        for (int i = 31; i >= 0; i--)
        {
                (num >> i) & 1 ? printf("1"):printf("0");
        }
        printf("\n");
}

o/p:
Enter a number
65530
00000000000000001111111111111010

Explanation:
Checking all the bits using "(num >> i) & 1" starting with bit 31 to bit 0.
Need to print bit 31(MSB) first as on the terminal it prints from left to right.
9) Will the below loop ever terminate?
#include <stdio.h>

#include <stdint.h>

int main()

{

     int cycle = 0;

     for(uint64_t itr=1; itr>0;)

     printf("itr=%llu cycle=%d \n", itr<<=1, ++cycle);
}

Options:
A) Yes, After 64 cycles
B) Yes, After 63 cycles
C) No, Infinite loop
D) Yes, Depends on CPU architecture

Explanation:
itr<<=1, After every iteration, 'itr' is left shifted by '1'.

Initially, itr(binary) 1 = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001

After iteration 1 itr = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000010

After iteration 2 itr = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000100

........

After iteration 63 itr = 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

After iteration 64 itr = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

So, After the 64th iteration, itr value becomes 0 and the loop will terminate.

Below is the o/p of the program.

itr=2 cycle=1
itr=4 cycle=2
itr=8 cycle=3
itr=16 cycle=4
itr=32 cycle=5
itr=64 cycle=6
itr=128 cycle=7
itr=256 cycle=8
itr=512 cycle=9
itr=1024 cycle=10
itr=2048 cycle=11
itr=4096 cycle=12
itr=8192 cycle=13
itr=16384 cycle=14
itr=32768 cycle=15
itr=65536 cycle=16
itr=131072 cycle=17
itr=262144 cycle=18
itr=524288 cycle=19
itr=1048576 cycle=20
itr=2097152 cycle=21
itr=4194304 cycle=22
itr=8388608 cycle=23
itr=16777216 cycle=24
itr=33554432 cycle=25
itr=67108864 cycle=26
itr=134217728 cycle=27
itr=268435456 cycle=28
itr=536870912 cycle=29
itr=1073741824 cycle=30
itr=2147483648 cycle=31
itr=4294967296 cycle=32
itr=8589934592 cycle=33
itr=17179869184 cycle=34
itr=34359738368 cycle=35
itr=68719476736 cycle=36
itr=137438953472 cycle=37
itr=274877906944 cycle=38
itr=549755813888 cycle=39
itr=1099511627776 cycle=40
itr=2199023255552 cycle=41
itr=4398046511104 cycle=42
itr=8796093022208 cycle=43
itr=17592186044416 cycle=44
itr=35184372088832 cycle=45
itr=70368744177664 cycle=46
itr=140737488355328 cycle=47
itr=281474976710656 cycle=48
itr=562949953421312 cycle=49
itr=1125899906842624 cycle=50
itr=2251799813685248 cycle=51
itr=4503599627370496 cycle=52
itr=9007199254740992 cycle=53

itr=18014398509481984 cycle=54
itr=36028797018963968 cycle=55
itr=72057594037927936 cycle=56
itr=144115188075855872 cycle=57
itr=288230376151711744 cycle=58
itr=576460752303423488 cycle=59
itr=1152921504606846976 cycle=60
itr=2305843009213693952 cycle=61
itr=4611686018427387904 cycle=62
itr=9223372036854775808 cycle=63
itr=0 cycle=64

Corerct Answer is : A) Yes, After 64 cycles

Leave a Comment