Ad
Code
Diff
  • public class Inverter
    {
      public static byte InvertBits(byte value)
      {
        uint uintValue;
        uintValue = value; 
        return (byte) ~uintValue;  
      }  
    }
    
    
    • unsigned char invertbits(unsigned char value)
    • public class Inverter
    • {
    • return value ^ 0xFF;
    • }
    • public static byte InvertBits(byte value)
    • {
    • uint uintValue;
    • uintValue = value;
    • return (byte) ~uintValue;
    • }
    • }

Invert all bits of a value and return the inverted value.

Examples:

invertbits(3) => returns 252 (11111100)
invertbits(255) => returns 0 (00000000)

unsigned char invertbits(unsigned char value)
{
   return value ^ 0xFF;  
}

Set a bit in value starting with bit 0 as the lowest bit.

Every bit value above 7 will be ignored and the function returns
the parameter value without a modification.

Examples:
setbit (3, 2) => returns: 7 (00000111)
setbit (3, 3) => returns: 11 (00001011)
setbit (3, 8) => returns: 3 (00000011)

unsigned char setbit (unsigned char value, unsigned int bit)
{
   return value | (1 << bit);
}