Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, June 30, 2020

Bitwise operators in C


Bitwise Operators in C
By Ahmad Irshad | C Programming Tutorial > Bitwise operaotrs in C

Bitwise operators are used to perform bit operations. Decimal numbers are converted into binary numbers which are the sequence of bits and bitwise operators work on these bits. 

Data in the memory (RAM) is organized as a sequence of bytes. Each byte is a group of eight consecutive bits. Bitwise operators are useful when we need to perform actions on bits of the data.

Bitwise operators work with integer type. They do not support float or real types.

There are six bitwise operators in C.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Bitwise left shift
>>Bitwise right shift
Bitwise AND (&) operator: 

The bitwise AND (&) operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

OperationResult
0 & 00
1 & 00
0 & 10
1 & 11
Note: Both operands to the bitwise AND operator must be of integer types. Since, bitwise operators work with integer type. They do not support float or real types.

Bitwise AND operator is used extensively to check whether a particular bit of data is on (1) or off (0).

Let's understand with a simple example:

Suppose x and y are two integer variables with initial value
int x=10, y=11;

Let us re-write these integers in 8-bit binary representation and then perform bit operation x & y.

  1. x = 0000 1010 // Binary representation of 10
  2. y = 0000 1011 // Binary representation of 11
  3. /* * Now let's perform bit operations x & y, by comparing each bit of
  4. * the first operand to the corresponding bit fo the second operand.
  5. */
  6. x 0000 1010
  7. y 0000 1011
  8. ---------------------------------------
  9. x & y 0000 1010

The above expression x & y will evaluate to 0000 1010 which is 10 in decimal.

Bitwise OR (|) operator: 

The bitwise OR | operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 0, the corresponding result bit is set to 0. Otherwise, the corresponding result bit is set to 1.

Bitwise OR | operator is commonly used to set flag bit values.

OperationResult
0 | 00
1 | 01
0 | 11
1 | 11
Let's understand with a simple example:

Suppose x and y are two integer variables with initial value
int x=3, y=14;

Let us re-write these integers in 8-bit binary representation and then perform bit operation x | y.

  1. x = 0000 0011 // Binary representation of 3
  2. y = 0000 1110 // Binary representation of 14
  3. /* * Now let's perform bit operations x | y, by comparing each bit of
  4. * the first operand to the corresponding bit fo the second operand.
  5. */
  6. x 0000 0011
  7. y 0000 1110
  8. ---------------------------------------
  9. x | y 0000 1111

The above expression x | y will evaluate to 0000 1111 which is 15 in decimal.

Bitwise XOR (^) operator: 

The bitwise XOR ^ operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits of the two operands are differ, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

OperationResult
0 ^ 00
1 ^ 01
0 ^ 11
1 ^ 10
Let's understand with a simple example:

Suppose x and y are two integer variables with initial value
int x=6, y=13;

Let us re-write these integers in 8-bit binary representation and then perform bit operation x ^ y.

  1. x = 0000 0110 // Binary representation of 6
  2. y = 0000 1101 // Binary representation of 13
  3. /* * Now let's perform bit operations x ^ y, by comparing each bit of
  4. * the first operand to the corresponding bit fo the second operand.
  5. */
  6. x 0000 0110
  7. y 0000 1101
  8. ---------------------------------------
  9. x ^ y 0000 1011

The above expression x ^ y will evaluate to 0000 1011 which is 11 in decimal.

Bitwise complement (~) operator: 

The bitwise complement ~ operator is also called one's complement operator since it always takes only one value or an operand. It is a unary operator. When we perform complement on any bits, all the one's become zero 0's and vice versa. 

OperationResult
~ 01
~ 10

Let's understand with a simple example:

Suppose x is an integer variable with initial value int x=10;

Let us re-write this integer in 8-bit binary representation and then perform one's complement operation ~x.

  1. x = 0000 1010 // Binary representation of 10
  2. /* * Now let's perform one's complement operations ~x,
  3. */
  4. x 0000 1010 // Binary representation of 10
  5. ---------------------------------------
  6. ~x 1111 0101 // one's comlement.

The above expression ~ x will evaluate to 1111 0101 which is --(Two's complement) in decimal.

Bitwise left shift (<<) operator: 

The bitwise left shift << operator is a binary operator. Which is used to shift each bits to left n times.

Let's understand with a simple example:

Suppose x is an integer variable with initial value int x=15;

Let us re-write these integers in 8-bit binary representation and the perform left shifting x << n.

  1. x = 0000 1111 // Binary representation of 15
  2. /* * Now let's perform left shifting x << n times.
  3. * Remember: shifting bits to left causes insertion of zero from right
  4. * and shifting each bit to one position left from its current position.
  5. * The (MSB) left most bit is dropped off on every left shift.
  6. */
  7. x 0000 1111 // Binary representation of 15 
  8. ---------------------------------------
  9. x << 1 0001 1110 // Sifting bits of x one times to the left. Which is =  30 in decimal
  10. x << 2 0011 1100 // Sifting bits of x two times to the left. Which is =  60 in decimal
  11. x << 3 0111 1000 // Sifting bits of x three times to the left. Which is =  120 in decimal
  12. x << 4 1111 0000 // Sifting bits of x four times to the left. Which is =  240 in decimal

The above expression x << 4 will evaluate to 1111 0000 which is 240 in decimal.

Remember: Shifting bits to left is also equivalent to multiplying value by 2. You can use bitwise left shift operator if you need to multiply a variable by a power of two. As you can see in the above example after each left shifting number becomes double.

Bitwise right shift (>>) operator: 

The bitwise right shift >> operator is a binary operator. Which is used to shift each bits to right n times.

Let's understand with a simple example:

Suppose x is an integer variable with initial value int x=31;

Let us re-write these integers in 8-bit binary representation and the perform right shifting x >> n.

  1. x = 0001 1111 // Binary reresentation of 31.
  2. /* * Now let's perform left shifting x >> n times.
  3. * Remember: shifting bits to right causes insertion of zero from left
  4. * and shifting each bit to one position left from its current position.
  5. * The left most bit is dropped off on every left shift.
  6. */
  7. x 0001 1111 // Binary representation of 31
  8. ---------------------------------------
  9. x >> 1 0000 1111 // Sifting bits of x one times to the rght.
  10. x >> 2 0000 0111 // Sifting bits of x two times to the right.
  11. x >> 3 0000 0011 // Sifting bits of x three times to the right.
  12. x >> 4 0000 0001 // Sifting bits of x four times to the right.

The above expression x >> 4 will evaluate to 0000 0001 which is -- in decimal.

After each right shift, the least significant bit (left most bit) is dropped off.

Remember: Shifting bits to right is also equivalent to dividing value by 2. You can use bitwise right shift operator if you need to divid a number (unsigned number) by a power of two.

Next topic >> Relational Operator.

Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us