Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconBitwise Operators in C [With Coding Example]

Bitwise Operators in C [With Coding Example]

Last updated:
26th Oct, 2020
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Bitwise Operators in C [With Coding Example]

Introduction

Operators are essential components of every programming language. They are the symbols that are used to achieve certain logical, mathematical, or other programming operations. C provides various operators for performing multiple operations, such as arithmetic, logical, and bit manipulation. There are eight different types of operators in C. These are:

  • Arithmetic Operators in C
  • Logical Operators in C
  • Conditional Operator in C
  • Relational Operators in C
  • Increment and Decrement Operators in C
  • Bitwise Operators in C
  • Assignment Operators in C
  • Special Operators in C

Check out our free courses to get an edge over the competition.

Explore Our Software Development Free Courses

In this article, you will learn about the Bitwise Operators in C and how to implement it.

What is a Bitwise Operator?

The Bitwise Operator in C is a type of operator that operates on bit arrays, bit strings, and tweaking binary values with individual bits at the bit level. For handling electronics and IoT-related operations, programmers use bitwise operators. It can operate faster at a bit level. 

Ads of upGrad blog

The Bitwise Operator in C performs its operation on the individual bits of its operand, where operands are values or expressions on which an operator operates. These operators are also used to perform the core actions as well as high-level arithmetic operations that require direct support of the processor. We can further subcategorize bitwise operators into three subtypes based on their working principles, logical (Bitwise AND, OR, and XOR), Shift (Right Shift and left shift), and Complement (Bitwise NOT).

Check out upGrad: Advanced Certification in Blockchain 

 There are six different Bitwise Operators in C. These are:

· Bitwise AND operator (&)

· Bitwise OR operator (|)

· Bitwise exclusive OR operator (^)

· Binary One’s Complement or Bitwise NOT operator (~)

· Bitwise Left shift operator (<<)

· Bitwise Right shift operator (>>)

Using bitwise operators, programmers can change the individual bits of any value contained in the operand. We can view a single byte of computer memory as 8-bits that signifies the true or false status of 8 flags. Bitwise operators are usually applied to define flag values in operating systems and driver software. For instance, in a file property, the read-only mode is conceptually expressed as a flag bit in the operating system, and the bitwise operator is used to toggle between the true and the false value.

Check out upGrad: Full Stack Development Bootcamp (JS/MERN)

Here is a table that shows how the computation of bitwise operators results.

Xyx & yx | yx ^ y
00000
01011
10011
11110

There are six different types of Bitwise Operators in C. These are:

The Bitwise AND (&) in C: The C compiler recognizes the Bitwise AND with & operator. It takes two operands and performs the AND operation for every bit of the two operand numbers. It is a binary operator. The output of this operator will result in 1 only if both bits are 1.

The Bitwise OR (|) in C: The C compiler recognizes the Bitwise OR with | operator. It takes two operands and performs the OR operation for every bit of the two operand numbers. It is also a binary operator. The output of this operator will result in 1 if any one of the two bits is 1.

The Bitwise XOR (^) in C: The C compiler recognizes the Bitwise XOR with ^ operator. It takes two operands and performs the XOR operation for every bit of the two operand numbers. It is also a binary operator. The output of this operator will result in 1 if both the bits have different values.

In-Demand Software Development Skills

Binary One’s Complement or Bitwise NOT operator (~) in C: The C compiler recognizes the Bitwise NOT with ~ operator. It takes only one operand and performs the inversion of all digits of it. It is a unary operator. The output of this operator will invert all the existing bits of that operand.

Bitwise Left shift operator (<<) in C: The C compiler recognizes the left shift operation with this <<. It takes only two operands and shifts all the bits of the first operand to the left. The second operand decides how many numbers of places this operator will shift its bits. It is a binary operator.

Bitwise Right shift operator (>>) in C: The C compiler recognizes the left shift operation with this >>. It takes only two operands and shifts all the bits of the first operand to the right. The second operand decides how many numbers of places this operator will shift its bits. It is a binary operator.

Explore our Popular Software Engineering Courses

Read: Operators in Python: A Beginner’s Guide to Arithmetic, Relational, Logical & More

Program for Bitwise Operator in C

Let us now take a look at the program using all the bitwise operators.

#include <stdio.h>

int main()

 {

unsigned char x = 20, y = 21; // x = 20 (00010100), y = 21 (00010101)

int g = 0;

   g = x & y; /* 20 = 010100 */

   printf(” The result of Bitwise AND is %d \n”, g );

   g = x | y; /* 21 = 010101 */

   printf(” The result of Bitwise OR is %d \n”, g );

   g = x ^ y; /* 1 = 0001 */

   printf(” The result of Bitwise XOR is %d \n”, g );

   g = ~x;

   printf(” The result of Bitwise NOT is %d \n”, g );

   g = x << 1;

   printf(” The result of Bitwise Left Shift is %d \n”, g );

   g = x >> 1;

   printf(” The result of Bitwise Right Shift is %d \n”, g );

return 0;

}

OUTPUT:

The result of Bitwise AND is 20

 The result of Bitwise OR is 21

 The result of Bitwise XOR is 1

 The result of Bitwise NOT is -21

 The result of Bitwise Left Shift is 40

 The result of Bitwise Right Shift is 10

Also Read: Python Tutorial: Setting Up, Tools, Features, Applications, Benefits, Comparison

Read our Popular Articles related to Software Development

Ads of upGrad blog

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

Conclusion

Bitwise operators are a particular type of operators in C used for bit-level programming. It is essential to know the use of the bitwise operator as it brings an efficient way of saving space in representing data. Programmers use these operators in various fields, such as systems programming, embedded programming, and designing protocols. Hence, programmers need to know its use. 

upGrad brings programming with C and a lot more with upGrad’s PG Diploma in Software Development Specialisation in Full Stack Development. A program to make you emerge as a full stack developer and learning to build some of the awesome applications. It is an extensive 12-months program that includes working on live projects and assignments and also training 15 programming languages and tools. Along with it, it has all-time career support with mock interviews and job assistance.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1Can you define the different types of operators in programming?

The different types of operators are arithmetic operators, assignment operators, increment and decrement operators, logical operators, comparison operators, and bitwise operators are all examples of operators. +, -, *, /, and % are arithmetic operators. They are used to conduct two-operand arithmetic operations. =, +=, -=, *=, /=, and percent = are the assignment operators. They are used to give a variable a value. ++ and — are increment operators. They are used to add or subtract one to a value. The operators for decrement are — and —. They're used to subtract one from a value. &&, ||, and! are logical operators. They are used to combine two logical expressions into one. ==,!=, >, >=, and = are comparison operators. They're utilised to make a comparison between two operands. &, |, and are bitwise operators. They're used to do two-operand bitwise operations.

2How do you make codes more efficient?

As much as feasible, keep the codes short. This means that the information is represented by the code using as few symbols as feasible. Ascertain that the codes are structured in a logical manner. This indicates that the codes are organised in a way that makes them simple to comprehend and apply. Make sure the codes are error-free. This indicates that the codes are error-free and may be utilised correctly. Check to see if the codes are up to date. This indicates that the codes represent the most recent information revisions and updates. Also, make sure the codes are simple to use. This indicates that the codes are simple to comprehend and apply.

3What are the applications of bitwise operators?

A group of operators known as bitwise operators work with individual bits in a number. AND, OR, XOR, NOT, SHIFT, and MASK are the 6 bitwise operators. Each bit in the number is regarded as a 0 or 1 by the operators, which work with the binary representation of numbers. They can be used to alter the sign of a number, set it to 0 or 1, move bits to the left or right, or combine two numbers to make a new one.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Top 7 Node js Project Ideas &#038; Topics
31545
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46899
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers &#038; Experienced]
901298
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners &amp; Experienced]
51980
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers &#038; Experienced]
909119
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34719
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers &#038; Experienced]
902367
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26128
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions &#038; Answers [2024]
4327
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon