BITWISE OPERATORS IN C

Maha

Bitwise Operators in C 

In C programming,the operators that operate on data at the bit level are known as bitwise operators. Programming at the bit level is also referred to as bitwise operations. It has two numbers, either 0 or 1.

  

Bitwise operators are:

& AND bitwise
|   OR bitwise
^ XOR bitwise
~ NOT bitwise
<<  left shift
>> right shift


Example for Bitwise AND operator:

Program:

#include<stdio.h>
int main() {
    int a = 8, b = 4;
    printf("Bitwise AND: %d\n",a&b); 
    return 0;
}

Output:

Bitwise AND: 0


Example for Bitwise OR operator:

Program:

#include<stdio.h>
int main() {
    int a = 8, b = 6;
    printf("Bitwise OR: %d\n",a|b); 
    return 0;
}

Output:

Bitwise OR: 14


Example for Bitwise XOR operator:

Program:

#include<stdio.h>
int main() {
    int a = 8, b = 6;
    printf("Bitwise XOR: %d\n",a^b); 
    return 0;
}

Output:

Bitwise XOR: 14


Example for Bitwise with left shift operator:

Program:

#include<stdio.h>
int main() {
    int a = 8, b = 5, c = 6;
    printf("Bitwise left shift: %d\n",c<<6); 
    return 0;
}

Output:


Bitwise left shift: 384


Example for Bitwise with right shift operator:

Program:

#include<stdio.h>
int main() {
    int a = 8, b = 5, c = 6;
    printf("Bitwise right shift: %d\n",c>>6); 
    return 0;
}

Output:

Bitwise right shift: 0


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send