Bitwise Operators in C: AND, OR, XOR, Shift & Complement

Bitwise Operators in C: AND, OR, XOR, Shift & Complement

31 Mar 2024
Beginner
2.23K Views
14 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Bitwise Operators in C: An Overview

By allowing you to manipulate individual bits within a byte of data, the bitwise operators will help us achieve incredible efficiencies in our programs with minimal effort. C Online Training will help you a lot if you're interested in mastering the bitwise operators in C. In this article on Learn C,we will take an in-depth look at what bitwise operators are, their syntax, and types; when they should be used, and some practical implementations.

What is a Bitwise Operator in C?

We already got somewhat familiar with the Bitwise Operators in the section Operators in C. We saw the definition and classification. Now we will dive more into these operators.

These operators work on individual bits. They enable programmers to directly manipulate a value by working on its bits. Arithmetic operations like addition, subtraction, multiplication, etc. are done on bits in the backend.

Bitwise operators can be used to test whether an expression is true or false, toggle certain bits, to clear bits, compare values of similar types, and shift right and left as needed.

They offer useful options for the manipulation of information stored in binary format, making them essential for critical operations ranging from basic calculations to complex memory operations in the C programming language.

Read More - Top 50 C Interview Questions and Answers

Types of Bitwise Operators in C

There are various types of bitwise operators in C, which are:

Types of Bitwise Operators in C

The truth table of a bitwise operator in C:

X Y X&YX/YX^Y
0 0 0 00
0 1 0 11
1 0 0 11
11111

  • Bitwise AND

This operator performs & operation on each bit of the two operands. The result is 1 if both the bits of the operand are 1 otherwise 0 in all other cases.

Example: Let’s perform bitwise & operation on 35 and 40 in the C Online Editor

/* 35 = 00100011 (In Binary)
 40 = 00101000 (In Binary)

Bit Operation of 35 and 40
 00100011
& 00101000
 ________
 00100000 = 32 (In decimal) */

#include <stdio.h>
int main() {
 int a = 35; // 00100011 in binary
 int b = 40; // 00101000 in binary
 int result = a & b; // 0100 in binary
 printf("%d & %d = %d\n", a, b, result);
 return 0;
}

Output

35 & 40 = 32

  • Bitwise OR

This operator performs | operation on each bit of the two operands. The result is 1 if either of the bits of the operands is 1 otherwise 0 in other cases.

Example: Let’s perform bitwise | operation on 5 and 3 

/* 5 = 00000101 (In Binary)
 3 = 00000011 (In Binary)

Bitwise OR Operation of 5 and 3
 00000101
| 00000011
 ________
 00000111 = 7 (In decimal) */

include <stdio.h>
int main() 
{
 int a = 5; // binary: 0101
 int b = 3; // binary: 0011
 int result = a | b; // binary: 0111
 printf("Result: %d\n", result); // Output: Result: 7
 return 0;
}

Output

Result: 7

  • Bitwise exclusive OR

The bitwise exclusive OR (XOR) operator, "^" takes two operands and after comparing them bit-by-bit, returns 1 only if corresponding bits of the two operands are opposite.

So, it can be used to perform binary addition or subtraction, check for single bits being on or off, and for quick flip-flopping between two different states in an application.

It can also act as a basis for other complex operations such as encrypting small strings of characters in C language with various ciphers or checking parity.

Example: Let’s perform bitwise ^ operation on 6 and 3 in the C Online Compiler

/* 6 = 00000110 (In Binary)
 3 = 00000011 (In Binary)

 Bitwise XOR Operation of 6 and 3
 00000110
^ 00000011
 ________
 00000101 = 5 (In decimal) */

#include <stdio.h>
int main() 
{
 unsigned int num1 = 6; // binary representation: 0110
 unsigned int num2 = 3; // binary representation: 0011
 unsigned int result = num1 ^ num2; // bitwise exclusive OR operation
 printf("%d ^ %d = %d\n", num1, num2, result); // output: 6 ^ 3 = 5
 return 0;
}

Output

0101

  • Bitwise Complement operator

It is a unary operator i.e. works on only one operand. It inverts all the bits of a number and changes the sign from positive to negative or vice versa. It is denoted by ~.

Example: Let’s perform bitwise ~ operation on 60
/* 60 = 00111100 (In Binary)

 Bitwise complement Operation of 60
~ 00111100
 ________
 11000011 = 195 (In decimal) */ 

#include <stdio.h>
int main() 
{
 unsigned int a = 60; // 60 is represented in binary as 0011 1100
 unsigned int b = ~a; // Bitwise complement of 60 is 1100 0011
 printf("The bitwise complement of %u is %u\n", a, b);
 return 0;
}

Output

The bitwise complement of 60 is 195

  • Bitwise shift operator
  • There are two types of shift operators in C:

    • Bitwise Left shift operator(<<)
    • Bitwise Right shift operator (>>)

    Example

    
    #include <stdio.h> int main() 
    { int x = 10; // decimal representation of 1010 in binary
     printf("x before shifting: %d\n", x);
     int y = x << 2; // left shift x by 2 bits
     printf("x after left shifting by 2 bits: %d\n", y); // output: 40
     int z = x >> 1; // right shift x by 1 bit
     printf("x after right shifting by 1 bit: %d\n", z); // output: 5
     return 0;
    }
    
    • In the above code, when x is left shifted by 2 bits, the value of x becomes 101000 in binary, which is equivalent to decimal 40.
    • When x is right-shifted by 1 bit, the value of x becomes 101 in binary, which is equivalent to decimal 5.

    Output

    x before shifting: 10 
    x after left shifting by 2 bits: 40 
    x after right shifting by 1 bit: 5
    

    Bitwise Operators in C vs. Logical Operators in C

    Bitwise OperatorsLogical Operators
    The programming language offers the bitwise operator kind of operator to carry out calculations. An operator type offered by the programming language to carry out logic-based operations is known as a logical operator.
    Bit-by-bit operations are carried out by bitwise operators, who operate on bits.A decision is made based on a number of conditions using logical operators.
    Integer operands are used by bitwise operators.Expressions that produce a boolean value are subject to the operations of logical operators.
    These are utilized for bit manipulation tasks like bit checking, bit setting, and bit clearing.These are used to determine whether the expression complies with certain requirements.
    Bitwise operations produce an integer value as their output.A boolean value, which can either be true or false, is the outcome of logical operators.

    Frequently Asked Questions

    1. What is Bitwise and logical and in C?

    Bitwise AND (&): When applied to individual bits of integers, bitwise AND (&) performs a binary AND operation. Results are 1 if both of the matching bits are 1, 0 otherwise. appropriate for bit manipulation.

    Logical AND (&&): The logical AND (&&) operator combines Boolean expressions, returning true (1) if both are true. used to make condition-based decisions.

    2. What is the difference between Bitwise and logical operators?

    Bitwise operations change the binary representation of integers by performing operations on specific bits. Boolean values are used by logical operators to evaluate the true or false nature of expressions. While logical operators manage program flow depending on circumstances, bitwise operators work with binary data.

    3. What is the & symbol in bitwise?

    In bitwise operations, the bitwise AND operator is represented by the & sign. It combines the equivalent bits of two integers using a binary AND operation. The outcome bit is 1 if both bits are 1, 0 otherwise. Programmers can manipulate bits at the bit level and mask data using this procedure.

    4. Why is bitwise XOR used?

    A binary number's bits can be toggled using bitwise XOR (). When used on two integer's corresponding bits, it yields a 1 if the bits are different and a 0 if they are the same. Because of this characteristic, XOR can be used for tasks like encoding data or flipping particular flags.

    Summary

    After understanding the principles of bitwise operators in C, it's time to master this complex concept. You'll get proficient with practice. Keep in mind that there is a connection between maths and computing below grammatical difficulties. Bitwise operators improve your coding abilities and set you apart from competing methods. Explore the bitwise operator program in C Certification Training to expand your knowledge of programming.

    Share Article
    Batches Schedule
    About Author
    Sakshi Dhameja (Author and Mentor)

    She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

    Self-paced Membership
    • 22+ Courses
    • 750+ Hands-On Labs
    • 300+ Quick Notes
    • 55+ Skill Tests
    • 45+ Interview Q&A
    • 10+ Real-world Projects
    • Career Coaching
    • Email Support
    Upto 66% OFF
    KNOW MORE..

    To get full access to all courses

    Accept cookies & close this