BitWise Operators

Let’s talk about bitwise operations this time. Broadly defined the bitwise operations are operations which take place at the level of individual bits. Typically the bitwise operations on almost all of the processors are faster than any of the arithmetic operations like ‘+’, ‘-‘, ‘*’, ‘/’

The bitwise operators give us the power to manipulate bits, the lowest level of data representation in any computer. Bitwise operators can be used to to speed up your program or to perform neat tricks.

There are six bitwise operators, ‘|’, ‘&’, ‘^’, ‘~’, ‘<<’, ‘>>’

1. ‘|’ Operator: The ‘|’ denotes the bitwise “or” operator. This is operator or’s all the individual bits of any two digits. It works same like the logical OR || operator.

The truth table is as follows:-

 Bit A   Bit B   Result A | B
  ------- ------- --------------
  0       1       1
  1       0       1
  1       1       1
  0       0       0

So you can see that the truth table is same as of logical || .

Now lets get to the fun part. We will find the | value of two integers.

Int    A    =   6 = 0110  
Int    B    =   9 = 1001  
So,   A|B   = 6|9 = 1111 = 15

The answer makes perfect sense.

2.’&’ Operator:‘&’ is the bitwise ‘and ‘ operator again it works the same as the logical AND operator but this one works on the bit sequence of two given bits. The truth table is given below:-

Bit A Bit B Result A & B
0 1 0
1 0 0
1 1 1
0 0 0

Okay, now once again let’s take a example with the slightly different digits.

Int    A    =   7      = 0111  
Int    B    =   9      = 1001  
So,    A&B  = 7&9      = 0001 = 1  

Again the answer makes perfect sense.

3. ‘^’ Operator: ‘^’ is the bitwise ‘eXclusive ORXOR operator. This is a slightly different operator. Here when we have an exact ‘or’ condition then only then the operator returns true, i.e. 0 ^ 1 will return 1 but 1 ^ 1 will not return 1. It will return 0.

The truth table is as follows:

Bit A Bit B Result A \^ B
0 1 1
1 0 1
1 1 0
0 0 0

Okay, now once again let’s take a example with the slightly different digits.

Int    A    =   7    = 0111  
Int    B    =   9    = 1001  
So,    A^B  = 7^9    = 1110  = 14

4. ‘~’ Operator: ‘~’ or ‘Tilde’ is the bitwise unary operator which operates only on a single bit. This operator perform the bit flipping, i.e. it forms the ones’ complement of a given bit sequence. In laymans’ terms thie operator turns 0’s to 1’s and 1’s to 0’s

The truth table is relatively simple

Bit A Result ~A
0 1
1 0

Let’s take an example to understand the ones’ complement clearly.

Int    A    =   7    = 0111  

So,   ~A    =  ~7    = 1000  = 8  

5. ‘<<’ Operator: ‘<<’ is the bitwise left shift operator. Technically, ‘<<’ this  is a bit shift operator as it shifts a given bit sequence to the left. Just like ones’ complement this operator works on a single bit sequence.

The generaly syntax of using ‘<<’ is

~~~~ {.example} [variable]<<[number of places] ~~~~

Whilst shifting, 0s are added as padding from the right.

Example-

Int    A        =   7         = 0111  

So,   A<<2  =  7<<2    = 11100   

6. ‘>>’ Operator:‘>>’ is the bitwise right shift operator. Works same as the left shift operator. But it shifts all the bits to the right.

Comments