Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers

# C Bitwise Operators - Aptitude Questions and Answers

**C programming Bitwise Operators Aptitude Questions and Answers: **In this section you will find C Aptitude Questions and Answers on Bitwise Operators like Bitwise OR (|), Bitwise AND (&), Bitwise NOT (!).

1) Which is not a bitwise operator?

- &
- |
- <<
- &&

**Correct answer: 4**

&&

&& is not a bitwise operator. It is a **Logical AND operator**, which is used to check set of conditions (more than one condition) together, if all conditions are true it will return 1 else it will return 0.

2) Predict the output of following program.

#include <stdio.h>
int main()
{
int a=10;
int b=2;
int c;
c=(a & b);
printf("c= %d",c);
return 0;
}

- c= 12
- c= 10
- c= 2
- c= 0

**Correct answer: 3**

c= 2

**Bitwise AND** (&) operator copies bit(s), if they are exist both of the operands. Here, binary of a is **"1010"** and binary of b is **"0010"**. Thus, result of expression (a & b) is **"0010"** which is equivalent to **2** in Decimal.

3) Predict the output of following program.

#include <stdio.h>
#define MOBILE 0x01
#define LAPPY 0x02
int main()
{
unsigned char item=0x00;
item |=MOBILE;
item |=LAPPY;
printf("I have purchased ...:");
if(item & MOBILE){
printf("Mobile, ");
}
if(item & LAPPY){
printf("Lappy");
}
return 1;
}

- I have purchased ...:
- I have purchased ...:Mobile, Lappy
- I have purchased ...:Mobile,
- I have purchased ...:Lappy

**Correct answer: 2**

I have purchased ...:Mobile, Lappy

**Bitwise OR** (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is **"0001"** and binary of Macro LAPPY (0x02) is **"0010"**, then result of the expression item |=MOBILE; will be **"0001"** and second expression item |=LAPPY; will return **"0011"**. Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.

4) Predict the output of following program.

#include <stdio.h>
int main()
{
char var=0x04;
var = var | 0x04;
printf("%d,",var);
var |= 0x01;
printf("%d",var);
return 0;
}

- 8,9
- 4,5
- 8,8
- 4,4

**Correct answer: 2**

4,5

Value of var is **0x04 (0100)**, Consider the expression var = var | 0x04 The ** OR (|) ** of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.

5) Predict the output of following program.

#include <stdio.h>
int main()
{
char flag=0x0f;
flag &= ~0x02;
printf("%d",flag);
return 0;
}

- 13
- d
- 22
- 10

**Correct answer: 1**

13

Consider the expression flag &= ~0x02 => flag = flag & (~0x02) => flag = 0x0f & (~0x02) => flag = D => flag =13.

6) Consider the given statement:

int x = 10 ^ 2

What will be the value of x?

- 5
- 6
- 7
- 8

**Correct answer: 4**

8

XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:

a b (a^b)
_______________________
0 0 0
0 1 1
1 0 1
1 1 0

Here, binary of **10** is **"1010"** and binary of **2** is **"0010"**, then the result of statement **(10 ^ 2)** will be **"1000"**, which is equivalent to **8** in decimal.

7) Predict the output of following program.

#include <stdio.h>
int main()
{
int x=10;
x &= ~2;
printf("x= %d",x);
return 0;
}

- x= 10
- x= 8
- x= 12
- x= 0

**Correct answer: 2**

x= 8

The statement x &= ~2; will clear second bit from the value of **10**, binary of **x** is **"1010"** and the binary of **2** is **"0010"**, thus this statement will clear second bit and returns **"1000"** that is equivalent to **8** in Decimal.

8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)

**Correct answer: 1**

Bitwise AND (&)

Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

**Consider the following program:**

#include <stdio.h>
int main()
{
int count;
for(count=1; count<=10; count+=1)
if(count & 1)
printf("%2d is ODD number\n",count);
else
printf("%2d is EVEN number\n",count);
return 0;
}

Output

1 is ODD number
2 is EVEN number
3 is ODD number
4 is EVEN number
5 is ODD number
6 is EVEN number
7 is ODD number
8 is EVEN number
9 is ODD number
10 is EVEN number

9) Which statement is suitable to check 3^{rd} (*count from 0*) bit is **high** (set) or not?

- (num & (1<<3))
- (num & 0x08)
- (num & 0x03)
- Both (1) and (2)

**Correct answer: 4**

Both (1) and (2)

The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3^{rd} bit of num is High (set) or not.

**Consider this program:**

#include <stdio.h>
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
if(num & (1<<3))
printf("3rd bit is High (Set)\n");
else
printf("3rd bit is Low\n");
return 0;
}

Output

First run:
Enter an integer number: 15
3rd bit is High (Set)
Second run:
Enter an integer number: 7
3rd bit is Low

Binary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3^{rd} bit is high and in second case 3^{rd} bit is low. [Count from 0]

10) Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

**Choose the correct words...**

- Multiplication and Division
- Division and Multiplication
- Multiplication and Remainder
- Remainder and Multiplication

**Correct answer: 1**

Multiplication and Division

Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.

**Consider this program:**

#include <stdio.h>
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
printf("Multiplication by 2 = %d\n", (num<<1));
printf("Division by 2 = %d\n",(num>>1));
return 0;
}

Output

Enter an integer number: 100
Multiplication by 2 = 200
Division by 2 = 50