Bitwise Operators in C tutorial and MCQ Questions
Table of Contents
Learn Programming Bitwise Operators in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
Bitwise Operators
C programming language provides various operators like Arithmetic, Logical, Conditional and Bitwise operators. Bitwise operators deal with Bits of data instead of Bytes of data.
Bitwise Operators in C
There are six Bitwise Operators in C.
- ~ TILDE
- << Left Shift
- >> Right Shift
- & Bitwise AND
- | Bitwise OR
- ^ Bitwise XOR ( Exclusive OR)
1. TILDE ~ Operator
TILDE ~ Operator is also called Bitwise One’s Complement Operator. 1’s complement operator simply replaces 1s with 0s and 0s with 1s.
Op1 | Result |
---|---|
0 | 1 |
1 | 0 |
//Binary of number 6 = 0110 int a= 6; printf("%d %d", a, ~a); //0110 -> 1001 //OUTPUT: 6 9
2. << Left Shift Operator
Left Shift operator shifts bits of a number from Right to Left. Gaps created on the Right side are filled with Zeroes. Bits on the left side are removed. So, <<N removes N bits on the left side.
//Binary of number 6 = 0110 int a = 6; printf("%d %d", a, a<<2); //Two Zeroes are filled right side. // 0110 -> 1000 //OUTPUT: 6 8
3. >> Right Shift Operator
Right Shift operator shifts bits of a number from Left to Right. Gaps created on the Left side are filled with Zeroes. Bits on the Right side are removed. So, >>N removes N bits on the Right side.
//Binary of number 6 = 0110 int a = 6; printf("%d %d", a, a>>2); //Two Zeroes are filled left side. // 0110 -> 0001 //OUTPUT: 6 1
4. & Bitwise AND Operator
Bitwise AND Operator & multiplies bit by bit. So 1 multiplied by 1 gives 0. Anything Multiplied by 0 gives 0.
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
//Binary of number 6 = 0110 //Binary of number 3 = 0011 int a = 6, b=3; printf("%d", a & b); //0010 //OUTPUT: 2
5. | Bitwise OR Operator
Bitwise OR Operator | gives a result of 1 if at least one input or operand is 1.
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
//Binary of number 6 = 0110 //Binary of number 3 = 0011 int a = 6, b=3; printf("%d", a | b); //0111 //OUTPUT: 7
6. ^ Bitwise Exclusive OR
Bitwise XOR Operator or Exclusive OR operator gives an output of 1 if two operands are different. That means, only 0 and 1 combination gives output as ONE.
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
//Binary of number 6 = 0110 //Binary of number 3 = 0011 int a = 6, b=3; printf("%d", a | b); //0101 //OUTPUT: 5
Bitwise Operators in C Examples
NO | Operator | Expression | Result |
---|---|---|---|
1 | ~ | ~1010 | 0101 |
2 | & | 1010 & 0011 | 0010 |
3 | | | 1010 | 0011 | 1111 |
4 | << | 1010 << 2 | 1000 |
5 | >> | 1101 >> 2 | 0011 |
6 | ^ | 1010 ^ 0010 | 1000 |
[WpProQuiz 47]
Bitwise Operators in C MCQ
1) What are Nibble, Word and Byte in computer language.?
A) Byte = 8 bits, Word= 4 Bytes, Nibble= 8 Bytes
B) Byte = 8 bits, Word=2 Bytes, Nibble=4 Bytes
C) Byte = 8 bits, Word=12 bits, Nibble=32 Bits
D) Byte = 8 bits, Word=24 bits, Nibble=40 Bits
Answer [=] B
2) Choose correct representation of Decimal number in Binary.
A)
0 0000 1 0001 2 0010 3 0011 4 0100
B)
5 0101 6 0110 7 0111 8 1000 9 1001
C)
10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F
D) All the above
Answer [=] D
Explanation:
A B C D E and F are HEX or Hexadecimal notation.
printf("%X", 10);//A printf("%x", 10);//a
3) What is the operator used to make 1’s One’s compliment.?
A) & Bitwise AND Operator
B) | Bitwise OR operator
C) ~ Bitwise Negate Operator
D) ^ Bitwise Exclusive OR
Answer [=] C
Explanation:
One’s complement is created by Negate Operator ~.
00010110 is changed to 11101001 after applying ~ operator.
4) What is the result of 0110 & 1100.?
A) 1000
B) 0100
C) 0001
D) 1010
Answer [=] B
Explanation:
Bitwise & operator gives 1 if both operands are 1. 1&1 = 1.
0&0=0 0&1=0 1&0=0 1&1=1
5) What is the output of Bitwise OR operation | on (0110 | 1100).?
A) 1110
B) 1100
C) 1000
D) 1010
Answer [=] A
Explanation:
Bitwise OR operation compares both operands. If at least one operand is 1 output is 1.
0|0=0 0|1=1 1|0=1 1|1=1
6) What is the output of Exclusive OR ^ operator on 0110^1000.?
A) 1000
B) 1110
C) 0011
D) 0001
Answer [=] B
Explanation:
Exclusive OR ^ compares both operands. If both operands are different, output is 1.
0^0=1 0^1=1 1^0=1 1^1=0
7) What is the output of Left Shift Operator << on (00011000<<2).?
A) 01100000
B) 11000000
C) 00000110
D) 00000011
Answer [=] A
Explanation:
Left Shift Operator << shifts bits on the left side and fills Zeroes on the Right end.
8) What is the result of Right Shift Operator >> on (00110000>>2).?
A) 11000000
B) 00001100
C) 01100000
D) 11001111
Answer [=] B
Explanation:
Right Shift Operator shift bits on the right side and fills Zeroes on the left side.
9) Choose correct statement about Left Shift Operator << .?
A) Left shift operator shifts individual bits on the left side
B) When shifting left side, overflow bits are ignored.
C) Zeroes are filled on the right side
D) All the above
Answer [=] D
10) Choose a correct statement about Right Shift Operator >> .?
A) Right shift operator shift individual bits on to the right side.
B) When shifting bits right side, overflow bits on the right are ignored or truncated.
C) Zeroes are filled on the left side.
D) All the above
Answer [=] D
11) What is a Single Operand Operator below.?
A) &
B) |
C) ^
D) ~
Answer [=] D
Explanation:
TILDE or Negation works on single Operand. ~1100=0011.
12) What is the Bitwise operator used to set a particular bit value to 1.?
A) & AND
B) | OR
C) ^ Exclusive OR
D) ~ Operator
Answer [=] B
Explanation:
To set a 3 bit to 0 from right in 0000001, OR with 00000100. Output is 00000101.
13) What is the Bitwise operator used set a particular bit to Zero 0.?
A) & Operator
B) | OR operator
C) ^ Exclusive Operator
D) ~ TILDE Operator
Answer [=] A
Explanation:
To set a 4th bit to 0, AND with 11110111.
00111000 & 11110111 =00110000;
14) Which is Bit Toggling operator below.?
A) & AND operator
B) | OR operator
C) ^ Exclusive Operator
D) ~ TILDE operator
Answer [=] D
15) Which is the format specifier used to prefix 0x and print a number in hexadecimal notation.?
A) %x
B) %0x
C) %#x
D) %hx
Answer [=] C
Explanation:
printf("%#x", 10); //output: 0xa
16) Left Shift operation is equivalent to.?
A) Division by 2
B) Multiplying by 2
C) Adding 2
D) Subtracting 2
Answer [=] B
Explanation:
Yes. (0001)<<1 = 0010. 1 x 2= 2 i.e 10 in binary.
17) Right Shift operation >> is equivalent to .?
A) Multiplying by 2
B) Division by 2
C) Adding 2
D) Subtracting 2
Answer [=] B
Explanation:
0100>>1 = 0010. So 4 / 2 = 2 = 10 in binary.
18) What is the minimum and maximum values in Octal Number System.?
A) 1 to 8
B) 0 to 7
C) 2 to 9
D) None of the above
Answer [=] B
Explanation:
10 in decimal = 12 in octal = 1100 in Binary.
19) What number system is not understood by C language compiler directly.?
A) Decimal
B) Octal
C) Binary
D) Hex Decimal
Answer [=] C
Explanation:
Yes. C language can not understand Binary language directly. Because C language is a High Level Language.
20) Choose a correct statement about C Bitwise operators.?
A) 0^number = number
B) 1 | number = number
C) 0 & number = 0
D) All the above
Answer [=] D