Bitwise Operator in C

Sakshi Dhameja  8 min read
12 Apr 2023
Beginner
204 Views

Introduction

Do you ever find yourself writing code and thinking how amazing it would be for a programming language to understand logical statements as easily as you do? Well, in C programming the bitwise operator gives computers that very capability. By allowing you to manipulate individual bits within a byte of data, the bitwise operators will help us achieve incredible efficiencies within our programs with minimal effort. In this article, I'm going to take an in-depth look at what bitwise operators are, their syntax and types, when they should be used, and some practical applications. So let's dive right into this exciting world of computer programming!

What are Bitwise Operators in C

Bitwise operators in C enable programmers to directly manipulate a value by working on its individual bits. Bitwise operators perform a logical operation on the value, one bit at a time, and are usually used in expressions with both an integer and a Boolean operator. 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. C has six bitwise operators: AND (&), OR (|), XOR (^), NOT (~), Bit Shift Right (>>), Bit Shift Left (<<). Bitwise operators 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.

Types of Bitwise Operators in C

There are various types of the bitwise operator in C, which are

Operator Meaning of operator
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise exclusive OR operator
~ One's complement operator (unary operator)
<<Left shift operator
>>Right shift operator

Truth table of 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 operator in C

Bitwise AND operator in C is an essential tool to master for computer programming. It involves combining two numbers by performing the binary & operator on each bit of the number. This Bitwise AND operator can be used for efficient memory and data access when manipulating large amounts of information in a program, allowing for fast bit-shifting or masking operations with large datasets. Bitwise operators are also useful for arithmetic calculations that require pre-defined data structures such as digits and byte sequences, providing the programmer with greater control over the organization of their code and operations, resulting in more efficient programs.

Example

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

Output

7 & 12 = 4

Bitwise OR operator in C

Bitwise OR operator in C, often denoted as "|", is a relatively simple and straightforward operation. It takes two binary values and produces a new binary value by combining them. Using Bitwise OR effectively relies on the programmer's understanding of how it works; the binary is only one of its possible applications. Bitwise OR can also be used to set single or multiple bits to 1 in a given pattern of bits, or to bit-shift data into the right positions for further calculations. With its wide range of uses and its simplicity compared to other operations, the Bitwise OR operator proves extremely useful when optimizing code.

Example

#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 operator in C

The bitwise exclusive OR (XOR) operator in C is an operator that takes two operands and compares them bit-by-bit, returning a result of 1 only if one bit of the given pair has a value of 1. Therefore, 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. Bitwise XOR can also act as a basis for other complex operations such as encrypting small strings of characters with various ciphers or checking parity. Bitwise XOR in C is easy to implement and an integral part of basic programming applications.

Example

#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 in C

The Bitwise Complement operator in C provides an easy way for the programmer to perform arithmetic operations on the bits of a number. This operator works by inverting all the bits of a number and changing the sign from positive to negative or vice versa. Bitwise Complement is especially useful when dealing with large numbers, as it allows for more efficient computation since it requires fewer operations than conventional methods do. Moreover, Bitwise Complement can also be used in comparison operations between parameters of different bit sizes. As C language evolves and newer versions are released, the Bitwise Complement operator will continue to prove its usefulness and enhance the efficiency of numeric computations.

Example

#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 4294967235

Bitwise shift operator in C

Bitwise shift operators in C allow programmers to manipulate the individual bits of an integer. This can be useful for setting, clearing, or testing bits without affecting other parts of the data stored in the integer. Bitwise shift operators are also used to quickly multiply and divide by powers of two for operations on bit strings or binary numbers. Bitwise shift operator syntax is simple yet powerful and can be used to create compact and efficient code. Bitwise shifting is important for implementing various algorithms and is essential for any programmer using C.

There are two types of shift operators in C, which are

    • Bitwise Left shift operator in C
    • Bitwise Right shift operator in C

    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, the << operator is used for the left shift, and the >> operator is used for the right shift.

    When x is left shifted by 2 bits, the value of x becomes 101000 in binary, which is equivalent to decimal 40. Therefore, the output of the program after the left shift is x after left shifting by 2 bits: 40.

    When x is right-shifted by 1 bit, the value of x becomes 101 in binary, which is equivalent to decimal 5. Therefore, the output of the program after the right shift is x right shifting by 1 bit: 5.

    Summary

     After learning all the basics about the bitwise operator and its application in C, it is time to conquer this seemingly complicated concept. With a few tweaks and ample practice, you will soon have the hang of it. Don’t forget that beneath the frequent syntax issues lies a deep connection between mathematics and computing! Learning bitwise operators also gives you an edge over other programming techniques with similar results. So make sure you go ahead with trying out this technique and take your coding skills to the next level. And if you’re looking for resources on mastering Bitwise Operator in C that are tailored to your experience level - you know where to find them. Just remember, stay curious, and keep coding!

    Accept cookies & close this