Bit operation in c

www.‮al‬utturi.com
Bit operation in c
int number = 199

Set the nth bit (set to 1)

number |= 1 << n;

Clear the nth bit (set to 0)

number &= ~(1 << n);

Invert the nth bit (1 to 0, 0 to 1)

number ^= 1 << n;

get the nth bit value:

bit = (number >> n) & 1;
Created Time:2017-08-22 15:23:04  Author:lautturi