C++ Programming

Bitwise operators in C/C++, AND, OR, XOR, left shift, right shift

Description:

Bitwise operators– In the C/C++ programming language, Operations can be performed on a bit level using bitwise operators. This is going to be a long article, as we will be doing all the calculations, in the end I will also share with you some C/C++ programs. I hope you will learn a lot from this article. If you have any questions, let me know in a comment.

Without any further delay let’s get started!!!

Amazon Purchase Links:

Top Gaming Computers

allpcb circuit

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Bitwise Operators:

The bitwise operators are used to access the computer hardware. Use of bitwise operators requires knowledge of different number systems and conversion of numbers from one system into another number system. The data inside the computer is represented in binary form, i.e. in a sequence of 0s and 1s. 8-bits are used to represent one character inside the computer. The bitwise operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network. The commonly used bitwise operators and their details are discussed below.


The Bitwise & (AND) Operator:

The bitwise AND operator is used to compare the contents of two operands (of int data type) on bit-by-bit basis. It returns 1 if he corresponding bit in both the operands is l otherwise it returns 0.

For example, if variable a = 10001001 and variable b = 01001000 then a & b is equal to 00001000.

i.e.

a =10001001

b= 01001000

a & b = 00001000

Example solve the problem using Bitwise & (AND) Operator

If a = 23, b = 13 and c = 11 and all are of integer type then what will be the result returned by the following expressions.

  1. a & b
  2. a & c
  3. c & b

To find out the results of the above expressions, the following steps are taken:

  • Convert decimal values of a, b and c into equivalent binary values.
  • Find out the result of expression in binary form.
  • Convert the result returned by the expression back into decimal form to get the final result.

Step-1: Convert decimal values of a, b and c into equivalent binary values:

Conversion of a:

A = 23

bitwise operators

Conversion of b:

B = 13

bitwise operators

Conversion of c:

C = 11

bitwise operators

So the value of a, b and c in binary form are:

(23)10 = (10111)2 = (0000000000010111)2

(13)10 = (1101)2 = (0000000000001101)2

(11)10 = (1011)2 = (0000000000001011)2

Since each integer value is represented inside the computer as two bytes (or 16bit), the binary conversion is shown in 16bits.



Step-2: Find out the result of expression in binary form

Find a & b:

a=  (0000000000010111)

b= (0000000000001101)

a & b=  (0000000000000101)

Find a & c

a=  (0000000000010111)

c=  (0000000000001011)

a & c =  (0000000000000011)

find c & b

c=  (0000000000001011)

b= (0000000000001101)

c & b = (0000000000001001)

Step-3: Convert the result returned by the expression back into decimal form to get the final result

Back conversion of a & b

a & b  =  (0000000000000101)2

= 22 x 1 + 21 x 0 + 20 x1

= 4 x 1 + 2 x 0 + 1 x 1

= 4 + 0 + 1

= 5

a & b  =  (5)10

Back conversion of a & c

a & c  =  (0000000000000011)2

= 21 x 1 + 20 x 1

= 2 x 1 + 1x 1

= 2+1

=3

a & c  =  (3)10

Back conversion of c & b

c & b = (0000000000001001)2

= 23 x 1 + 22 x 0 + 21 x 0 + 20 x 1

= 8  + 0 + 0 + 1

= 9

C & b = (9)10


Example how to implement bitwise operator & (AND) in C++ programming:

The Bitwise | (OR) Operator:

The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit by bit basis. It returns 1 of any one of the two bits is 1, otherwise it returns 0. It is represented by the vertical bar ( | ).

I.e.

If variable a = 10001001 and variable b = 01001000 then a | b is equal to 00001000

A= 10001001

B= 01001000

A | b = 11001001

Example: solve the problem using Bitwise | (OR) Operator

Suppose a = 23, b = 13, and c = 11 and all are of integer type then what will be the result returned by the following expression

  1. a | b
  2. a | c
  3. c | b

To find out the results of the above expressions, the following steps are taken:

  • Convert decimal values of a, b and c into equivalent binary values.
  • Find out the result of expression in binary form.
  • Convert the result returned by the expression back into decimal form to get the final result.

Step-1:Convert decimal values of a, b and c into equivalent binary values:

Conversion of a:

A = 23

bitwise operators

Conversion of b:

B = 13bitwise operators

 Conversion of c:

C = 11

bitwise operators

 Step-2: Find out the result of expression in binary form

Find   a | b:

A = (0000000000010111)

B = (0000000000001101)

a | b=  (0000000000011111)

Find a | c

A = (0000000000010111)

C = (0000000000001011)

a | c = (0000000000011111)

Find c | b

c = (0000000000001011)

b = (0000000000001101)

c | b = (0000000000001111)


 Step-3: Convert the result returned by the expression back into decimal form to get the final result

Back conversion of a | b

a | b  =  (0000000000011111)2

=   24 x 1 + 23 x 1 + 22 x1 +21 x 1+20 x 1

=   16 + 8 + 4 + 2 + 1

= 31

a | b  =  (31)10

Back conversion of a | c

a | c  =  (0000000000011111)2

= 24 x 1 + 23 x 1 + 22 x1 +21 x 1 + 20 x 1

= 16 + 8 + 4 + 2 + 1

= 31

a | c  =  (31)10

 Back conversion of c | b

c | b = (0000000000001111)2

= 23 x 1 + 22 x 1 + 21 x 1 + 20 x 1

= 8 + 4 + 2 + 1

= 15

C | b = (15)10

 Example how to implement bitwise operator | (OR) in C++ programming :


The Bitwise ^ (XOR) Operator:

The bitwise XOR operator is used to compare the contents to two operands (of int data type) one bit-by-bit basis. It returns 1 if the bits in the corresponding operands are different otherwise it returns 1 (if corresponding bits are same). It is represented by the caret ( ^ ) sign.

If variable a = 10001001 and variable b = 01001000 then a | b is equal to 00001000

A= 10001001

B= 01001000

A ^ b = 11000001

Example: solve the problem using Bitwise ^ (XOR) Operator

Suppose a = 23, b = 13, and c = 11 and all are of integer type then what will be the result returned by the following expression

  1. a ^ b
  2. a ^ c
  3. c ^ b

To find out the results of the above expressions, the following steps are taken:

  • Convert decimal values of a, b and c into equivalent binary values.
  • Find out the result of expression in binary form.
  • Convert the result returned by the expression back into decimal form to get the final result.

Step-1: Convert decimal values of a, b and c into equivalent binary values:

Conversion of a:

A = 23

bitwise operators

Conversion of b:

B = 13

bitwise operators

Conversion of c:

C = 11

bitwise operators

Step-2: Find out the result of expression in binary form

Find   a ^ b:

A = (0000000000010111)

B = (0000000000001101)

a ^ b =  (0000000000011010)

Find a ^ c

A = (0000000000010111)

c = (0000000000001011)

a ^ c = (0000000000011100)

Find c ^ b

c = (0000000000001011)

b = (0000000000001101)

c ^ b = (0000000000000110)



Step-3: Convert the result returned by the expression back into decimal form to get the final result

Back conversion of a ^ b

a ^ b  =  (0000000000011010)2

= 24 x 1 + 23 x 1 + 22 x 0 +21 x 1+20 x 0

= 16 + 8 + 0 + 2 + 0

= 26

a ^ b  =  (26)10

Back conversion of a ^ c

a ^ c  =  (0000000000011100)2

= 24 x 1 + 23 x 1 + 22 x 1 +21 x 0 + 20 x 0

= 16 + 8 + 4 + 0 + 0

= 28

a ^ c  =  (28)10

Back conversion of c ^ b

c ^ b = (0000000000000110)2

= 22 x 1 + 21 x 1 + 20 x 0

= 4 + 2 + 0

= 6

C ^ b = (6)10

Example how to implement bitwise operator ^ (XOR) in C++ programming :


The Bitwise ~ (complement) Operator:

The bitwise complement operator is used to operate on an expression. It inverts the bits of the expression. It takes each bit of the operand and converts it to 1 if it is a 0 and to 0 it if is 1. It is represented by the tilde (~) sign.

If variable a = 10001001 then

a= 10001001

~a= 01110110

Example: solve the problem using Bitwise ~ (complement) Operator

Suppose a = 23, b = 13, and c = 11 and all are of integer type then what will be the result returned by the following expression

  1. ~ (a + b)
  2. ~ (a ^ c)
  3. ~ (c | b)

To find out the results of the above expressions, the following steps are taken:

  • Convert decimal values of a, b and c into equivalent binary values.
  • Find out the result of expression in binary form.
  • Convert the result returned by the expression back into decimal form to get the final result.

Step-1: Convert decimal values of a, b and c into equivalent binary values:

Conversion of a:

A = 23

bitwise operators

 Conversion of b:

B = 13

bitwise operators

 Conversion of c:

C = 11

bitwise operators

 Step-2: Find out the result of expression in binary form

Find ~(a + b)

a = (0000000000010111)

b = (0000000000001101)

a + b = (0000000000100100)

~ (a + b) = (1111111111011011)

Find ~ (a ^ c)

a = (0000000000010111)

c = (0000000000001011)

a ^ c = (0000000000011100)

~ (a ^ c) = ( 1111111111100011)

Find ~ (c | b)

c           = (0000000000001011)

b           = (0000000000001101)

c | b      = (0000000000001111)

~ (c | b) = (1111111111110000)


Step-3: Convert the result returned by the expression back into decimal form to get the final result

back conversion of ~(a + b)

~(a+b)= (1111111111011011)2

=  215 x 1 + 214 x 1 + 213 x 1 +212 x 1 +211 x 1+210 x 1+29x 1+28 x 1+27 x 1+26 x 1+25 x 1+24 x 1+23 x 1+22 x 1+21 x 1+20 x 1

=32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128+ 64+ 0 + 16 +8+0+2+ 1

(65499)10

~(a + b) = 65499

back conversion of ~(a ^ c )

~ (a ^ c)=( 1111111111100011)2

=  215 x 1 + 214 x 1 + 213 x 1 +212 x 1 +211 x 1+210 x 1+29x 1+28 x 1+27 x 1+26 x 1+25 x 1+24 x 0+23 x 0+22 x 0 +21 x 1+20 x 1

=32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128+ 64+ 32 + 16 +0+0+0+ 0

=(65507)10

~ (a ^ c) = (65507)10

 Back conversion of ~(c | b)

~ (c | b) = ( 1111111111110000)2

=  215 x 1 + 214 x 1 + 213 x 1 +212 x 1 +211 x 1+210 x 1+29x 1+28 x 1+27 x 1+26 x 1+25 x 1+24 x 1+23 x 0+22 x 0 +21 x 0+20 x 0

= 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128+ 64+ 32 + 16 +0+0+0+ 0

= (65520)10

~ (c | b) = (65520)10

Example how to implement bitwise operators ~ (complement) in C++ programming :


The Left-Shift (<<) bitwise Operators:

The left shift operator is used to shift a specified number of bits of an operand to the left. It has two operands. The first operand on the left hand-side of the operator is the constant or variable the operator is the constant or variable whose bits are to be shifted. The second operand on the right-hand-side of the operator specified the number of bits that are to be shifted to the left.

When the specified number of bits of the operand are shifted to the left then the same number of bits on the right are filled with 0s.

The left-shift operator is represented by the (<< ) sign.

If variable a = 1111100110001001 then a << 5 will be:

A = 1111100110001001

a << 5 = 0011000100100000

In the above example, five leftmost bits, i.e. 11111 are shifted and five 0s are filled on the left.

Example solve the problem using Bitwise << (left shift) Operator

Suppose a=23, b=13 all of integer type then what will be returned by the following:

  • a <<3
  • b <<10
  • a << 1

To find out the results you must have to perform three steps.

  • Convert the decimal values of a. b and c into binary.
  • Find out the value in binary of operand after shifting the bits.

Convert the value of operand into decimal form to get the final result.

Step-1: Convert decimal values of a, b and c into equivalent binary values:

Conversion of a:

a = 23

bitwise operators

 

Conversion of b:

b = 13

bitwise operators

 Step-2: Find out the result of expression in binary form

Find a<<3

a         =   (0000000000010111)

a<< 3 =   (0000000010111000)

 find b<< 10

b           = (0000000000001101)

b << 10 = (0011010000000000)

find a << 1

a      = (0000000000010111)

a << 1= (0000000000101110)



Step-3: Convert the result returned by the expression back into decimal form to get the final result

Back conversion of a<<3

a <<3 = 27 x 1 +26 x 0+25 x 1+24 x 1+23 x 1+22 x 0+21 x 0+20 x 0

= 128+0+32+16+8+0+0+0

a <<3 =(184)10

Back conversion of b << 10

b << 10 = 213x 1 + 212 x 1 + 211 x 0 + 210 x 1 + 29 x 0

=8192 + 4096 +0 + 1024 +0 +0

b << 10  = (13312) 10

Back conversion of a << 1

a << 1 = 25 x 1+ 24 x 0 +23+1 + 22 x 1 +21 x 1 +20 x 0

=32 +0 +8 +4 +2 + 0

a << 1 = (46) 10

Example how to implement bitwise operators left shift (<< ) in C++ programming :


The Right-Shift (>>) bitwise Operators:

The right shift operator is used to shift a specified number of bits of an operand to the right. It has two operands. The first operand at the left hand side of the operator is the constant or variable whose bits are to be shifted. The second operand at the right hand side specified the number of bits that are to be shifted to the right. when the specified number of the bits of the operand are shifted to the right then the same number of bits on the left are filled with 0s

The right shift bitwise operators is represented by the (>>) sign

If variable a = 1111100110001001 then a >> 5 will be:

a          = 1111100110001001

a >> 5 = 0000011111001100

In the above example five bits i.e. 01001 on the right are shifted and five 0s are filled on the left.

Example solve the problem using  >> (right shift) Bitwise Operators

Suppose a=23, b=13 all of integer type then what will be returned by the following:

  1. a >> 3
  2. b >> 10
  3. a >> 1

To find out the results you must have to perform three steps.

  • Convert the decimal values of a, b into binary.
  • Find out the value in binary of operand after shifting the bits.

Convert the value of operand into decimal form to get the final result.

Step-1: Convert decimal values of a, b into equivalent binary values:

Conversion of a:

a = 23

bitwise operatorsConversion of b:

b = 13

bitwise operators

Step-2: Find out the result of expression in binary form

Find a >> 3

a         = (0000000000010111)

a >> 3 = (0000000000000010)

Find b >> 10

b           = (000000000001101)

b >> 10 = (000000000000000)

Find a >> 1

a      = (0000000000010111)

a >> 1= (0000000000001011)


Step-3: Convert the result returned by the expression back into decimal form to get the final result

Back conversion of a>>3

a>> 3 = 21 x 1 + 20 x 0

= 2 + 0

= (2)10

Back conversion of b >> 10

b >> 10 = (0)10

Back conversion of a >> 1

a >> 1 = 23 x 1 + 22 x 0 + 21 x 1 + 20 x 1

= 8 + 0 + 2 + 1

= (11)10

Example how to implement bitwise operator Right shift (>> ) in c++ programming :

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button