C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Bitwise OR

  Bitwise OR works almost exactly the same way as bitwise AND. The only difference is that only one of the two bits needs to be a 1 for that position's bit in the result to be 1. (If both bits are a 1, the result will also have a 1 in that position.) The sybmol is a pipe: |. Again, this is similar to Boolean logical operator, which is ||. 

For example, 0x56 | 0x32 is 0x76, because: 

 


   0 1 0 1 0 1 1 0
 | 0 0 1 1 0 0 1 0
  -----------------
   0 1 1 1 0 1 1 0


The ^ (caret) operator performs a bitwise exclusive-OR on two integers. Each bit in the result is 1 if one, but not both, of the corresponding bits in the two input operands is 1. For example, 0x56 ^ 0x32 is 0x64: 


    0 1 0 1 0 1 1 0
^  0 0 1 1 0 0 1 0
    ---------------
    0 1 1 0 0 1 0 0


The ~ (tilde) operator performs a bitwise complement on its single integer operand. (The ~ operator is therefore a unary operator, like ! and the unary -, &, and * operators.) Complementing a number means to change all the 0 bits to 1 and all the 1s to 0s. For example, assuming 16-bit integers, ~0x56 is 0xffa9: 


0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0
    -------------------------------
    1 1 1 1 1 1 1 1 1 0 1 0 1 0 0 1



The << operator shifts its first operand left by a number of bits given by its second operand, filling in new 0 bits at the right. Similarly, the >> operator shifts its first operand right. If the first operand is unsigned, >> fills in 0 bits from the left, but if the first operand is signed, >> might fill in 1 bits if the high-order bit was already 1. (Uncertainty like this is one reason why it's usually a good idea to use all unsigned operands when working with the bitwise operators.) For example, 0x56 << 2 is 0x158


For example, 0x56 << 2 is 0x158:


   0 1 0 1 0 1 1 0 << 2
  -------------------
   0 1 0 1 0 1 1 0 0 0


  And 0x56 >> 1 is 0x2b: 
 

  
  0 1 0 1 0 1 1 0 >> 1
  ---------------
  0 1 0 1 0 1 1


For both of the shift operators, bits that scroll ``off the end'' are discarded; they don't wrap around. (Therefore, 0x56 >> 3 is 0x0a.) 

The bitwise operators will make more sense if we take a look at some of the ways they're typically used. We can use & to test if a certain bit is 1 or not. For example, 0x56 & 0x40 is 0x40, but 0x32 & 0x40 is 0x00: 


   0 1 0 1 0 1 1 0 0 0 1 1 0 0 1 0
& 0 1 0 0 0 0 0 0 & 0 1 0 0 0 0 0 0
   --------------- ---------------
   0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0


 Since any nonzero result is considered ``true'' in C, we can use an expression involving & directly to  test some condition, for example: 


  if(x & 0x04)
     do something ;

 

Back Next
 

Google