Bit Manipulation with examples, Left, right Shift, ith bit..

 Bit Manipulation



Types of Bitwise Operators
  1. Binary AND &
  2. Binary OR |
  3. Binary XOR ^
  4. Binary Ones's Complement ~
  5. Binary Left Shift <<
  6. Binary Right Shift >>

Binary And &

1 & 0=0
0 & 0=0
1 & 1=1
0 & 1=0

Example

5 & 7
=> 101 & 111= 101
 => 5

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(5 & 7)<<endl;




return 0;
}

OUTPUT: 5

Binary OR |

1 | 0=1
0 | 0=0
1 | 1=1
0 | 1=1

Example

5 & 7
=> 101 & 111= 111
 => 7

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(5 | 7)<<endl;




return 0;
}

OUTPUT: 7


Binary XOR ^ (Exclusive OR)

1&0=1
0&0=0
1&1=0
0&1=1

Example

5 ^ 7
=> 101 ^ 111= 010
 => 2

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(5 ^ 7)<<endl;




return 0;
}

OUTPUT: 2

Binary NOT ~ 

~ 0  = 1
~ 1 = 0

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(~5)<<endl;




return 0;
}

OUTPUT: -6

Binary Left Shift << ( Multiplication )

Generally written as 5 << 2
as 5 is written as 0000101 in Binary so
5 << 2 = 0010100 i.e the number gets multiplied by 2^2
Left Shift means moving the every bit left.

Example

5 << 2
=> 20

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(5<<2)<<endl;




return 0;
}

OUTPUT: 20


Binary Right Shift >>( Division)

Generally written as 5  >> 1
as 5 is written as 0000101 in Binary so
5 >> 1 = 000010 i.e the number gets divide by 2^1
Right Shift means moving the every bit right.

Example

5 >> 1
=> 2

#include<bits/stdc++.h>
using namespace std;
int main() {

    cout<<(5>>2)<<endl;




return 0;
}

OUTPUT: 2

Check whether a number is ODD or EVEN using Bitwise Operator

If the LSB (Least Significant Bit) is 1  then taking & with 1 will give 1 only so the number will be odd and if LSB is 0 then taking & with 1 will give 0 so it means it is Even.

#include<bits/stdc++.h>
using namespace std;
int main() {

    int n= 100;
    int t=101;
    if(n&1){
        cout<<"100 is ODD"<<endl;
    }
    else{
        cout<<"100 is EVEN"<<endl;
    }
    if(t&1){
        cout<<"101 is ODD"<<endl;
    }
    else{
        cout<<"101 is EVEN"<<endl;
    }

return 0;
}

OUTPUT:
100 is EVEN 101 is ODD

Tags:
bitwise
left shift
right shift
bit manipulation
bit manipulation in c++ with examples
bit manipulation coding
left shift operator
right shift operator
left shift operator in c
bit manipulation in c
python bit manipulation
right shift operator in java
left shift and right shift
logical shift left
left shift operator in python
bit manipulation python
bitwise left shift operator
left shift in c
bitwise left shift
bit manipulation java
logical shift right
arithmetic shift left operation
right shift in c
right shift operator in python
left shift c++
arithmetic shift left
c++ left shift
bit manipulation gfg
shift right testing
python left shift
left shift and right shift in c
leetcode bit manipulation
bit manipulation c
python right shift
vhdl shift left
bit manipulation tricks
c++ right shift
bit manipulation leetcode
left shift right shift
c++ bit manipulation
java left shift
shift to right
c shift left
binary left shift
binary left shift operator
java right shift
bit manipulation in c++
right shift and left shift
shift left vhdl
shift left in c
right shift in python
bit shift right
arduino bit manipulation
binary manipulation
shift left operator
right shift python
c++ shift left
shift left binary
binary shift left
bit manipulation hackerearth
bitwise left and right shift operator in python
shift left is equal to
bitwise right shift operator in python
left and right shift
verilog shift left
signed right shift
shift left python
python left shift operator
shift left operator in c
left and shift
bitwise left shift operator in python
right shift in c++
bit manipulation for competitive programming
hackerearth bit manipulation
shift left logical mips
shift left verilog
bitwise 100
shift left 2
shiftleft inc
shift left operation
bit manipulation tutorial
bitwise 20
circular shift left
c# left shift
rshift python
shift right python
21 shares bitwise
shiftleft security
c# shift left
arduino shift left
vhdl shift left std_logic_vector
shift left bit
shift left paradigm
bitwise aum
interviewbit bit manipulation
c bit shift left
in bit manipulation
javascript shift left
shift left in vhdl
shiftleft pricing

Post a Comment

0 Comments