Create a function to invert bit from an integer - C Function

C examples for Function:Utility Function

Description

Create a function to invert bit from an integer

Demo Code

#include <stdio.h>

unsigned invert(unsigned x, int p, int n) {
   // 000001111100000
   unsigned mask = ~(~0U << n) << (p + 1 - n);

   // xxxxx^^^^^xxxxx
   return x ^ mask;
}

int main(void) {
   printf("%u\n", invert(5432, 6, 3));
   return 0;// w  ww .  j ava2s. c  o m
}

Related Tutorials