Use a mask to select multiple bits from a variable. - C Operator

C examples for Operator:Bit Operator

Description

Use a mask to select multiple bits from a variable.

Demo Code

#include <stdio.h>

int main(void){
  unsigned int original = 0xABC;
  unsigned int result = 0;
  unsigned int mask = 0xF;          // Rightmost four bits

  printf("\n original = %X", original);

  // Insert first digit in result
  result |= original & mask;        // Put right 4 bits from original in result
  printf("\t result = %X\n", result);

  // Get second digit
  original >>= 4;                   // Shift original right four positions
  result <<= 4;                     // Make room for next digit
  printf("\t result = %X\n", result);
  
  result |= original&mask;          // Put right 4 bits from original in result
  printf("\t result = %X\n", result);

  /* Get third digit */
  original >>= 4;                   // Shift original right four positions
  result <<= 4;                     // Make room for next digit
  printf("\t result = %X\n", result);
  
  result |= original & mask;        // Put right 4 bits from original in result
  printf("\t result = %X\n", result);
  return 0;/*from www.  ja va  2 s.  c o  m*/
}

Result


Related Tutorials