Bit-Wise Operator

0




Q.) What are Bits?

Ans.)A bit (short for binary digit) is the smallest unit of data in a computer. A bit has a single binary value, either 0 or 1. Although computers usually provide instructions that can manipulate bits through BitWise operator.


Q.How to manipulate the Bits?

Ans.) Manipulates the Bits through BitWise operators. there are various kind of operations in Bitwise operations i.e Binary left Shift ( << ), Binary Right Shift (>>), Compliment Operator ( ~ ), Binary And ( &) ,Binary  OR (), Binary XOR ( ^ ).


1.) Binary Left Shift ( << ):
In which bits move towards left and changes the operator value or variable value.

In the above example, you able to see how bits are shifted toward left. In this above example, only one bit shifted.

Code:-


2.)Binary Right Shift( >> ):
It's similar to Left Shift operator but in which shift moves towards Right instead of Left.

In the above example, you able to see how bits are shifted toward left. In this above example, only one bit shifted.



3.) Bitwise AND Operator ( & ):
In this operator AND operation performed and it's like a logical AND.
A B A & B
0 0 0
0 1 0
1 0 0
1 1 1
In our machine or program, compiler converted our program to binary code that file is like .bat,.exe,.out.This file while you open then
you able to see machine instructions instead of your normal code so If we performed AND operation between 2 operands then its works on Bits
Like:-
int i=5;
int k=10;
( i & k)=> 0

if we perform 5 & 10 then we get 0 because the representation of 10 in binary is 1010 and 5 in binary is 0101.
And when we perform the AND operation between those then we perform the AND operation of every single bit of 5 and 10 correspondingly
and according to the above table, we get 1 or 0;



4.) Bitwise Not Operator ( ~ ):
In Bitwise Not Operator, it's like the Compliment of the operator.In another word 1's Compliment of the operand and One bit is showing the sign (+/-) that is also changed when we use this operator.


CODE

EXECUTION





5.)Bitwise OR Operator( | ):
In OR operator we perform an operation on bits instead of the number and this process is similar to BITWISE AND OPERATION.
But its table is little bit changed shown in below:
A B A | B
0 0 0
0 1 1
1 0 1
1 1 1
if we perform between 2 operands then its works on bits and checks each and every bit of both operands correspondingly


int i=5;
int k=10;
(i | k)=> 15

CODE

EXECUTION







6.)Bitwise XOR Operator( ^ ):
In Bitwise XOR Operator, Xor operation performed between bits of the two operands and if the value of the bits are same then the result becomes ZERO and if bits value are not similar then the result is ONE.


In the above table you able to understand the result after XOR operation                                       
                             

CODE

EXECUTION

                 







Post a Comment

0Comments

We you have any doubt let me know :)

Post a Comment (0)
To Top