Showing posts with label bitwise operators. Show all posts
Showing posts with label bitwise operators. Show all posts

Thursday, November 14, 2013

Bitwise operator ^ (XOR)

The bitwise operator ^ (XOR) which is also called exclusive or, is used as shown in the chart.

^   0   1
0   1   0
1   0   1

So as we can see the bit1^bit2 equals to 1 if bit1=bit2 else it equals to 0.

Bitwise operator | (OR)

Just like we discussed bitwise operator &, we will discuss this operator with the help of statement if.

Statement if for || (OR)

     condition 1       condition 2     result
          false                   false           false
            true                   false           true
          false                   true             true
         true                     true              true

So, just like the if statement , the operator | t\does the same but with bits.

example
000001110100
|
000000011100
=
000001111110



Bitwise operator & (and)

The operator & is another bitwise operator int C++. For making let's call the bits names. 0-false and 1 -true.
First let's look at an example
000000011100
&
000000000101
Let's remember the logical operator if. Just like the && (and) operator in if , the bitwise operator & (and) has the same meaning. When we had a conditional operator if and 2 conditions in it .
Let's have a loot at this chart
for operator && in if statement
    condition 1   condition 2     result
          false            false           false
          true             false           false
          false            true            false
          true             true            true
So basically , if both of the conditions are true => the if statement is true otherwise its false. Just like && the operator & does the same but with bits.

for bitwise operator &
      bit1    bit2    result
         0       0         0
         1       0         0
         0       1         0
         1       1         1

Remember that 0 stands for false and 1 stands for true
 So in our example from the beginning , when we perform the operator & we get

000000011100
&
000000000101
=
000000000100



Wednesday, November 13, 2013

bitwise poerators << and >>

Bit is a digit of a binary representation of a number. In C++ there are operators referring to bits.
 One of those operators are the operators >> and <<. If we try to translate the operator << into english we would get this definition move the current bits to the right. And the >> stands for move the bits to the left. The operator is mostly used for multiplying the number with the power of 2.  Now we will use an example to make it clear. 

Image somewhere in our memory we have the decimal number 3 which is 11 in binary.

1 2 3 4 5 6 7 8
0 0 0 0 0 1 1 0
The start point is the position 6 and the endpoint is 7.  Now if apply 3<<2 (means move 2 bits to the left)
we will get this
1 2 3 4 5 6 7 8
0 0 0 1 1 0 0 0
NOTE that the endpoint is going to stay the same . Its 7 but the start point moved to position 4. We sure know that binary 1100 is 12 which is 3*4.
So    n<<m=n*2^m


and   n>>m=n/2^m