Bitwise Operators (Part 1)(AND ,OR , NOT)

Hello Guys, In This Article We are discussed about Bitwise Operators in C Language.

In This Article, Here we are introducing bitwise operators and what are the bitwise operators then after we are going to talk about bitwise AND bitwise OR and bitwise NOT operator.

let's try to understand,

what are bitwise operators?

what are bitwise Operators As the Name Suggests it does bitwise Manipulation now, what is the meaning of bitwise Manipulation we will discuss this thing letter in the Article for as of now we are focusing on what are the bitwise operators available in C language okay.

Types of bitwise operators

1. AND (&)
2. OR (|)
3. NOT(~)
4. Left Shift (<<)
5. Right Shift (>>)
6. XOR(^)

Bitwise AND(&) Operator

let's understand what is bitwise AND operator so,
1. bitwise AND operator takes two bit at a time and performs operations. 
2. AND is a binary operator it takes two numbers and performs bitwise AND operation
notice here AND is binary Operator.
3. The result of AND is 1 when both bits are 1.make sure you have to focus on this point. The result of AND is 1 when Both bits are 1.

Example:-

suppose, we have decimal numbers 7 and 4.
Here is the binary representation of 7(0111) and here is a binary representation of 4(0100) okay.
so as we have seen when we perform AND operation when both bits 1 then the only result will be 1.
so you can see 1 and 0 one bit is 1  and another bit is 0 so the result will be 0.
similarly, we have another bit to 1 and 0 then the result will be 0.
we have both the bits are 1 then the result will be 1.
as we see the result of AND is 1 when both bits are 1.So here both bits are 1 we can get 1. and both bits is 0 it's simply 0.

7        0 1 1 1
4    & 0 1 0 0 
      -------------
4        0 1 0 0

as you convert these bits into decimal it will give you an 4 as a result.

Here is a Truth table for bitwise AND operator.
Both bits are 0 which means 0.
one bit is 0 and another is 1 it also takes 0.
one 1 and another is 0 then it will be 0.
both the bits are 1 then only it will 1.

Bitwise OR(|) Operator

let's talk about bitwise OR Operator.
it takes two bits at a time and performs OR operation.
OR is a binary operator it takes to the number and performs bitwise OR.
result of OR is 0 when both the bits are 0 look at this point in bitwise AND operator What we have the result of AND is 1 when both bits are 1.
over here, we have the result of OR is 0 When both the bits are 0.

so, let's look at the example of this here is an example which is discussed in bitwise AND but it may given different output for bitwise OR Operator
Let's look at this, Here is a binary representation of 7(0111), and here is a binary representation of 4(01000). 
so, what we have a result for both the bits are 0 then the only result is 0.so both the bits are 0 then we have result 0.
in all other cases, we have put simply 1 okay.

7        0 1 1 1
4      | 0 1 0 0
       -------------
7        0 1 1 1
so we can say that 7 bitwise OR 4 is equal to 7.

Here is a Truth table for bitwise OR operator.
so both bits are 0 then only we gonna put 0,
otherwise we simply just put 1 here.

Bitwise NOT(~) Operator

let's look about not the operator.NOT is unary Operator. it's job is to complement each bit one by one.
what is mean by. it just simply convert 0 to 1 and 1 to 0.
The result of NOT is 0 when a bit is 1 and 1 when a bit is 0.
here it will say when the bit is 1 it will convert into 0.and when the bit is 0 it will convert into 1.

let's look at the example here as the binary representation of 7(0111) when we perform NOT operation on it.
7    ~ 0 1 1 1
------------------
8       1 0 0 0

it will give you as a result of 8(1000). 1 is 0 and 0 will convert into 1. so here you can see we have 1 bit it will convert 0, similarly 1 convert 0, similarly 0 convert 1 and we get 8 in decimal.
so here is a truth table of NOT operator 
0 it converts 1 and 1 it converts 0 okay.

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b;
    a=7;
    b=4;

    printf("Bitwise AND(&) = %d\n", a&b);
    printf("Bitwise AND(|) = %d\n", a|b);
}

OUTPUT
Bitwise AND(&) = 4
Bitwise AND(|) = 7



Comment below if you have any queries related to the above tutorial for Bitwise Operators in C.

Post a Comment

0 Comments