Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Bitwise Operators in C

We use the bitwise operators in C language to perform operations on the available data at a bit level. Thus, performing a bitwise operation is also called bit-level programming. It is mainly used in numerical computations for a faster calculation because it consists of two digits – 1 or 0.

In this article, we will take a look into the Bitwise Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Types of Bitwise Operators in C

There are various types of bitwise operators used in all the programming languages. Here is a list of the ones used in C:

  • Bitwise OR Operator
  • Bitwise AND Operator
  • Unary Operator (Binary One’s complement operator)
  • Bitwise XOR Operator
  • Binary Right Shift Operator
  • Binary Left Shift Operator

The table below lists all the Bitwise operations that are supported by the C language. Let us assume that the variable ‘Q’ holds 13 while a variable ‘P’ holds 60, then:

Operator Meaning Description Examples
| Bitwise OR operator It copies a bit when it exists in either of the operands. (P | Q) = 61, which is, 0011 1101
& Bitwise AND operator It copies a bit to the result when it exists in both the operands. (P & Q) = 12, which is, 0000 1100
~ Binary Ones complement operator It is a unary operator that has an effect to ‘flip’ the bits. Meaning, all the 0s become 1s and vice-versa. (~P ) = ~(60), which is,. 1100 0011
^ Bitwise XOR operator It is an exclusive OR operator that copies the bit when it is set in one of the operands, but not in both. (P | Q) = 61, which is, 0011 1101
>> Shift operator (Right) It moves the value of the left operand to the right by the number of bits that the right operand specifies. P >> 2 = 15 which is, 0000 1111
<< Shift operator (Left) It moves the value of the right operand to the left by the number of bits that the right operand specifies. P << 2 = 240 which is, 1111 0000

Example of Bitwise Operators in C

Let us look at the following example to understand how the bitwise operators work in the C language:

#include <stdio.h>

main() {

unsigned int p = 60; /* 60 = 0011 1100 */

unsigned int q = 13; /* 13 = 0000 1101 */

int r = 0;

r = p | q; /* 61 = 0011 1101 */

printf(“Line 1 – The value of r is %d\n”, r );

r = p & q; /* 12 = 0000 1100 */

printf(“Line 2 – The value of r is %d\n”, r );

r = ~p; /*-61 = 1100 0011 */

printf(“Line 3 – The value of r is %d\n”, r );

r = p ^ q; /* 49 = 0011 0001 */

printf(“Line 4 – The value of r is %d\n”, r );

r = p >> 2; /* 15 = 0000 1111 */

printf(“Line 5 – The value of r is %d\n”, r );

r = p << 2; /* 240 = 1111 0000 */

printf(“Line 6 – The value of r is %d\n”, r );

}

The compilation and execution of the program mentioned above will produce the result as −

Line 1 – The value of c is 61

Line 2 – The value of c is 12

Line 3 – The value of c is -61

Line 4 – The value of c is 49

Line 5 – The value of c is 15

Line 6 – The value of c is 240

Here is another example regarding how we can use the bitwise operators in the C language:

// C Program to demonstrate use of bitwise operators

#include <stdio.h>

int main()

{

// p = 5(00000101), q = 9(00001001)

unsigned char p = 5, q = 9;

// The result is 00000001

printf(“p = %d, q = %d\n”, p, q);

printf(“p&q = %d\n”, p & q);

// The result is 00001100

printf(“p^q = %d\n”, p ^ q);

// The result is 00001101

printf(“p|q = %d\n”, p | q);

// The result is 11111010

printf(“~p = %d\n”, p = ~p);

// The result is 00000100

printf(“q>>1 = %d\n”, q >> 1);

// The result is 00010010

printf(“q<<1 = %d\n”, q << 1);

return 0;

}

The output generated from above will be −

p = 5, q = 9

p&q = 1

p^q = 12

p|q = 13

~p = 250

q>>1 = 4

q<<1 = 18

Some Interesting Facts to Consider

1. The right shift operators and left shift operators must not be used for the negative numbers –

When the second (that describes the total number of shifts) operand is a negative number, we witness an undefined behavior in C. For instance, the results that we get from both 1 >> -1 and 1 << -1 are undefined. As a matter of fact, if the number gets shifted to be more than the size of the available integer, then also the behavior will be undefined. For instance, 1 << 33 will be undefined if the storing of integers occurs using 32 bits. Also, we cannot perform any shift operations if the additive-expression operand (the operand that decides the total number of shifts) is 0.

Note – This type of behavior is very well-defined in the C++ language.

2. From a perspective of technical interview, the bitwise XOR operator is basically the most useful one –

This operator is used in various problems. One of the examples here can be “With a set of numbers in which all the available elements occur even many times except one of the numbers, find the number that is oddly occurring”. This type of problem can be solved pretty effectively if we do the XOR of all these numbers.

3. We cannot use the bitwise operators in the place of any logical operator –

The result that we get from a logical operator (&&, ! and ||) is either 1 or 0. But the bitwise operators, on the other hand, return a value that is an integer. Along with this, a logical operator would consider any operand that is non-zero to be 1. Let us consider an example of a program where the results of && and & are different for the very same operands.

4. The bitwise operators should not be used in place of logical operators –

Logical operators (&&, || and !) generate results of either 0 or 1. The bitwise operator, on the other hand, returns a value or integers. Also, the logical operators consider any non-zero operand as 1. For example, consider the following program, the results of & and && are different for the same operands.

#include <stdio.h>

int main()

{

int a = 2, b = 5;

(a & b) ? printf(“False “) : printf(“True “);

(a && b) ? printf(“False “) : printf(“True “);

return 0;

}

The generated output would be:

False True

5. The right-shift and left-shift operators are equivalent to respectively division and multiplication by 2. But it will work only when the available numbers are positive.

Let us look at an example,

#include <stdio.h>

int main()

{

int a = 19;

printf(“a >> 1 = %d\n”, a >> 1);

printf(“a << 1 = %d\n”, a << 1);

return 0;

}

The generated output would be:

a >> 1 = 9

a << 1 = 38

6. We can use the & operator to check quickly if a number is even or odd –

The value of the given expression would be non-zero (x & 1) if x is odd. In other cases, the value will be zero.

Let us look at an example,

#include <stdio.h>

int main()

{

int x = 19;

(x & 1) ? printf(“Even”) : printf(“Odd”);

return 0;

}

The generated output would be:

Even

7. We have to use the ~ operator very carefully –

The result obtained from the ~ operator on any small number could be a big number in case we store the result in an unsigned variable. Also, the result can be a negative number when this result is stored in a variable that is signed.

Practice Problems on Bitwise Operators in C

1. How can we flip or toggle any particular bit in a number?

Answer – For toggling any bit in a given variable, we have to use the exclusive Or / XOR (^ ) operator.

2. Which of the following bitwise operators works best for turning a particular off in any given number?

A. & operator

B. && operator

C. ! operator

D. || operator

Answer – A. & operator

3. Which of the following bitwise operators works best for turning a particular on in any given number?

A. & operator

B. && operator

C. | operator

D. || operator

Answer – C. | operator

4. Which of the following bitwise operators works best for checking if a particular bit is off or on?

A. & operator

B. && operator

C. ! operator

D. || operator

Answer – A. & operator

5. Which of these is not a bitwise operator?

A. &

B. |

C. <<

D. &&

Answer – D. &&

It is not a bitwise operator, but a logical AND operator. We use the && operator to check the sets of conditions together (two or more conditions). In case all the conditions are true, it will return 1. In any other case, it will return to 0.


FAQs

Q1

Can we use both the shift operators- right and left- combined in a code?

Yes, we can combine the shifts operators, and then we can use these to extract available data from an integer expression.

Q2

Does the complementary operator flip the entire sequence of code?

No, it doesn’t. It rather flips the 1s and 0s. It means that all the 0s become 1s and vice-versa.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*