Return to Snippet

Revision: 2691
at March 27, 2007 19:27 by rengber


Updated Code
//Bitwise Operations

//Turn a bit on
int startVal = 4;                 //100 
int bitVal = 2;                   //010
int newVal = startVal | bitVal;   //110 (6) 
//Alternatively
startVal |= bitVal; 
                  
//Turn a bit off
int startVal = 7;                 //111 
int bitVal = 5;                   //101
int newVal = startVal & bitVal;   //101 (5) 
//Alternatively
startVal &= bitVal; 


//Query bit status
int startVal = 6;                 //110  
int bitVal = 4;                   //100
int newVal = startVal & bitVal;   //100 
if(newVal != 0)
{
  //Bit was on.  
}

int startVal = 6;                 //110  
int bitVal = 1;                   //001
int newVal = startVal & bitVal;   //000 
if(newVal == 0)
{
  //Bit was off.  
}

Revision: 2690
at March 27, 2007 19:18 by rengber


Initial Code
//Bitwise Operations

//Turn a bit on
int startVal = 4;                 //100 
int bitVal = 2;                   //010
int newVal = startVal | bitVal;   //110 (6) 
                  
//Turn a bit off
int startVal = 7;                 //111 
int bitVal = 5;                   //101
int newVal = startVal & bitVal;   //101 (5) 

//Query bit status
int startVal = 6;                 //110  
int bitVal = 4;                   //100
int newVal = startVal & bitVal;   //100 
if(newVal != 0)
{
  //Bit was on.  
}

int startVal = 6;                 //110  
int bitVal = 1;                   //001
int newVal = startVal & bitVal;   //000 
if(newVal == 0)
{
  //Bit was off.  
}

Initial URL

                                

Initial Description

                                

Initial Title
Bitwise operations on Integers

Initial Tags

                                

Initial Language
C#