UrbanPro
true

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Bit wise operators in C

Venkateswararao S.
18/01/2017 0 0

Bit Wise Operators

          Bit Wise operators are manipulates of individual bits with in a word of memory. The bit wise operators can be divided in to three general category.

  1. One’s Complement Operator
  2. Logical bit wise Operator
  3. Shift Operator

One’s Complement Operator:

          The one’s complement operator (~) is a unary operator that causes the bits of its operand to be inverted so that 1s become 0s and 0s become 1s. this operator always precedes its operand. The operand must be an integer – type quantity. Generally, the operand will be an unsigned octal or an unsigned hexadecimal quantity.

Ex:-   consider the hexadecimal number 07ff. the corresponding bit pattern, expressed in terms of a 16 – bit word, is 0000 0111 1111 1111. So that one’s complement of this bit pattern is

          1111 1000 0000 0000

             f        8      0       0       which corresponds to the hexadecimal number f800.

Ex:-   #include<stdio.h>

main( )

{

                   unsigned i=0x5b3c;

                   printf("Hexadecimal values:i=%x ~i=%x\n",i,~i);

                   printf("Hexadecimal equivalents: i=%u ~i=%u",i,~i);

}

Logical bit wise operators:

          There are three logical bit wise operators

  1. bitwise and (&)
  2. bitwise exclusive or (^)
  3. bitwise or (i)

 

The operations are carried out independently on each pair of corresponding bits within the operands. Thus, the least significant bits (i.e., the rightmost bits) within the two operands will be compared, then the next least significant bits, and so on, until all of the bits have been compared. The results of these comparisons are:

  1. A bitwise and expression will return a 1 if both bits have a value of 1

(i.e., if both bits are true). Otherwise, it will return a value of 0.

  1. A bitwise exclusive or expression will return a 1 if one of the bits has a value of 1 and the other has a value of 0 (one bit is true, the other false). Otherwise, it will return a value of 0.
  2. A bitwise or expression will return a 1 if one or more bits have a value of 1 ( one or both bits are true). Otherwise, it will return a value of 0.

 

Table                                                           Logical Bitwise operators 

b1

b2

b1 & b2

b1 ^ b2

b1 | b2

1

1

0

0

1

0

1

0

1

0

0

0

0

1

1

0

1

1

1

0

 

Ex:-   a and b are unsigned variables whose values are 0x6db7 and 0xa726

one’s complement of a and b

 

a=6db7                                              b=a726

  a= 0110 1101 1011 0111                  b= 1010 0111 0010 0110

~a= 1001 0010 0100 1000                ~b= 0101 1000 1101 1001

         9       2       4       8                              5       8      d       9

~a= 0x9248                                       ~b=0x58d9

bitwise and                                                 bitwise exclusive or

a= 0110 1101 1011 0111                  a= 0110 1101 1011 0111                 

b= 1010 0111 0010 0110                  b= 1010 0111 0010 0110

  a & b= 0010 0101 0010 0110            a ^ b= 1100 1010 1001 0001

                 2      5       2       6                              c       a       9       1

a & b= 0x2526                                  a ^ b= 0xca91

          bitwise or ( | )

a= 0110 1101 1011 0111                                    

b= 1010 0111 0010 0110

  a | b = 1110 1111 1011 0111

                 e       f        b       7

          a | b = 0xefb7

          Each of the logical bitwise operators has its own precedence. The bitwise and (&) operator has the highest precedence, followed by bitwise exclusive or (^), then bitwise or ( | ). Bitwise and follows the equality operators (== and !=). Bitwise or is followed by logical and (&&). The associativity for each bitwise operator is left to right.

The Shift Operators:

          The two bitwise shift operators are shift left (<<) and shift right (>>). Each operator requires two operands. The first is an integer – type operand that represents the bit pattern to be shifted. The second is an unsigned integer that indicates the number of displacements. This value cannot exceed the number of bits associated with the word size of the first operand.

          The left shift operator causes all of the bits in the first operand to be shifted to the left by the number of positions indicated by the second operand. The leftmost bits in the original bit pattern will be lost. The rightmost bit positions that become vacant will be filled with 0s.

          The right shift operator causes all of the bits in the first operand to be shifted to the right by the number of positions indicated by the second operand. The rightmost bits in the original bit pattern will be lost. If the bit pattern being shifted represents an unsigned integer, then the leftmost bit positions that become vacant will be filled with 0s. Hence, the behavior of the right shift operator is similar to that of the left shift operator when the first operand is an unsigned integer.

Ex:-   a is an unsigned integer variable, whose value is 0x6db7. The expression

          b = a<<6; (left shift operator)

              |lost bits|

          a= 0110 1101 1011 0111

                          

       
   
     
 

 

 

               shift left

       
       

 

 

 a << 6= 0110 1101 1100 0000         = 0x6dc0

                                   | 0s     |

          b= a >> 6;

                                    |lost bits|

          a= 0110 1101 1011 0111

 
   

 

 

         

 

                         shift right

       
   
     
 

 

 

 

A >> 6 = 0000  0001  1011  0110 = 0x01b6

              |  0s     |

 

 

Ex:-   #include<stdio.h>

main( )

{

                   unsigned a= 0xf05a;

                   int b=a;

                   printf("%u %d\n", a,b);

                   printf("%x\n", a>>6);

                   printf("%x\n",b>>6);

}

Bitwise Assignment Operators:

          C also contains the following bitwise operators.

                   &=     ^=      |=      <<=    >>=   

          These operators combine the preceding bitwise operations with ordinary assignment. The left operand must be an assignable integer – type identifier, and the right operand must be a bitwise expression. The left operand is interpreted as the first operand in the bitwise expression. The value of the bitwise expression is then assigned to the left operand.

 

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Some Tricky Questions in C
Can you write two functions in which one executes before main function and other executes after the main function? #pragma startup #pragma exit Where, priority is optional integer value.For user priority...

Can we store different data types in a stack?
Yesterday, one of my Facebook friend asked me this question. My answer is "yes", and in this post I will discuss how could we do this.I am a great supporter of working with unions and I will be using union...

What is Safety-Critical Software?
A software whose failure may cause death or injuries to the users called Safety-Critical Software.The software present as part of Aeroplanes, or present in Rail as railway signalling software or Automotive...

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

INTRODUCTION TO PROGRAMMING LANGUAGES
Language is a medium for communication. If we want to perform anything with another person, we can know the human language as a human being. Similarly, if we want to perform anything with a computer, we...
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more