Bitwise Operators in c




Bitwise Operators in C




Table of Contents:



  1. Introduction to Bitwise Operators

  2. AND Operator (&)

  3. OR Operator (|)

  4. XOR Operator (^)

  5. Complement Operator (~)

  6. Left Shift Operator (<<)

  7. Right Shift Operator (>>)

  8. Bitwise Operators Examples and Programs

  9. Summary




1. Introduction to Bitwise Operators


In C programming, bitwise operators are used to manipulate individual bits of data. These operators perform operations at the bit level, enabling efficient handling of flags, masks, and other bitwise operations. There are six bitwise operators in C:



  • AND Operator (&)

  • OR Operator (|)

  • XOR Operator (^)

  • Complement Operator (~)

  • Left Shift Operator (<<)

  • Right Shift Operator (>>)


In this post, we will explore each bitwise operator and understand how they work with examples and programs.




2. AND Operator (&)


The AND operator, denoted by &, performs a bitwise AND operation between two operands. It sets a bit in the result only if the corresponding bits of both operands are set (1).




























A B A & B
0 0 0
0 1 0
1 0 0
1 1 1

Example:
Let's consider two variables num1 and num2, and perform the bitwise AND operation:



#include <stdio.h>

int main() {
unsigned int num1 = 12; // binary: 1100
unsigned int num2 = 9; // binary: 1001
unsigned int result = num1 & num2;

printf("Result: %u\n", result); // Output: Result: 8 (binary: 1000)

return 0;
}



3. OR Operator (|)


The OR operator, denoted by |, performs a bitwise OR operation between two operands. It sets a bit in the result if either of the corresponding bits of the operands is set (1).




























A B A | B
0 0 0
0 1 1
1 0 1
1 1 1

Example:
Let's consider two variables num1 and num2, and perform the bitwise OR operation:



#include <stdio.h>

int main() {
unsigned int num1 = 12; // binary: 1100
unsigned int num2 = 9; // binary: 1001
unsigned int result = num1 | num2;

printf("Result: %u\n", result); // Output: Result: 13 (binary: 1101)

return 0;
}



4. XOR Operator (^)


The XOR operator, denoted by ^, performs a bitwise XOR (exclusive OR) operation between two operands. It sets a bit in the result if the corresponding bits of the operands are different.




























A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

Example:
Let's consider two variables num1 and num2, and perform the bitwise XOR operation:



#include <stdio.h>

int main() {
unsigned int num1 = 12; // binary: 1100
unsigned int num2 = 9; // binary: 1001
unsigned int result = num1 ^ num2;

printf("Result: %u\n", result); // Output: Result: 5 (binary: 0101)

return 0;
}



5. Complement Operator (~)


The complement operator, denoted by ~, performs a bitwise NOT operation on a single operand, which inverts all the bits.


Example:
Let's consider a variable num and perform the bitwise complement operation:



#include <stdio.h>

int main() {
unsigned int num = 12; // binary: 1100
unsigned int result = ~num;

printf("Result: %u\n", result); // Output: Result: 4294967283 (binary: 11111111111111111111111111110011)

return 0;
}



6. Left Shift Operator (<<)


The left shift operator, denoted by <<, shifts the bits of the left operand to the left by the number of positions specified by the right operand. It effectively multiplies the left operand by 2 raised to the power of the right operand.


Example:
Let's consider a variable num and perform the left shift operation:



#include <stdio.h>

int main() {
unsigned int num = 10; // binary: 1010
unsigned int result = num << 2;

printf("Result: %u\n", result); // Output: Result: 40 (binary: 101000)

return 0;
}



7. Right Shift Operator (>>)


The right shift operator, denoted by >>, shifts the bits of the left operand to the right by the number of positions specified by the right operand. For unsigned data types, the vacant positions are filled with zeros, while for signed data types, the vacant positions are filled with the sign bit (0 for positive numbers, 1 for negative numbers).


Example:
Let's consider a variable num and perform the right shift operation:



#include <stdio.h>

int main() {
int num = 25; // binary: 11001
int result = num >> 2;

printf("Result: %d\n", result); // Output: Result: 6 (binary: 110)

return 0;
}



8. Bitwise Operators Examples and Programs


Here are some additional examples and programs demonstrating the use of bitwise operators in C:



  1. Example: Checking if a number is even or odd using bitwise AND


  2. #include <stdio.h>

    int main() {
    int num = 17;
    if (num & 1) {
    printf("%d is odd.\n", num);
    } else {
    printf("%d is even.\n", num);
    }

    return 0;
    }

  3. Program: Swapping two numbers using XOR


  4. #include <stdio.h>

    void swap(int *x, int *y) {
    *x = *x ^ *y;
    *y = *x ^ *y;
    *x = *x ^ *y;
    }

    int main() {
    int a = 10, b = 20;
    printf("Before swapping: a = %d, b = %d\n", a, b);
    swap(&a, &b);
    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;
    }




9. Summary


In this post, we explored the bitwise operators in C: AND, OR, XOR, complement, left shift, and right shift. We learned how these operators manipulate individual bits and perform various bitwise operations. Understanding bitwise operators is crucial for low-level programming, bitwise manipulation, and optimizing certain algorithms.


Feel free to experiment with bitwise operators in your C programs to gain a deeper understanding of their behavior and applications.


Happy coding!





Post a Comment

0 Comments