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:-
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 |
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
int k=10;
( i & k)=> 0
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:
if we perform between 2 operands then its works on bits and checks each and every bit of both operands correspondingly
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
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 |
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 |
---|---|
We you have any doubt let me know :)