How to set, clear, and toggle a single bit?
As this is tagged "embedded" I'll assume you're using a microcontroller. All of the above suggestions are valid & work (readmodifywrite, unions, structs, etc.).
However, during a bout of oscilloscopebased debugging I was amazed to find that these methods have a considerable overhead in CPU cycles compared to writing a value directly to the micro's PORTnSET / PORTnCLEAR registers which makes a real difference where there are tight loops / highfrequency ISR's toggling pins.
For those unfamiliar: In my example, the micro has a general pinstate register PORTn which reflects the output pins, so doing PORTn = BIT_TO_SET results in a readmodifywrite to that register. However, the PORTnSET / PORTnCLEAR registers take a '1' to mean "please make this bit 1" (SET) or "please make this bit zero" (CLEAR) and a '0' to mean "leave the pin alone". so, you end up with two port addresses depending whether you're setting or clearing the bit (not always convenient) but a much faster reaction and smaller assembled code.
Use the bitwise OR operator (
) to set a bit.
number = 1UL << n;
That will set the n
th bit of number
. n
should be zero, if you want to set the 1
st bit and so on upto n1
, if you want to set the n
th bit.
Use 1ULL
if number
is wider than unsigned long
; promotion of 1UL << n
doesn't happen until after evaluating 1UL << n
where it's undefined behaviour to shift by more than the width of a long
. The same applies to all the rest of the examples.
Use the bitwise AND operator (&
) to clear a bit.
number &= ~(1UL << n);
That will clear the n
th bit of number
. You must invert the bit string with the bitwise NOT operator (~
), then AND it.
The XOR operator (^
) can be used to toggle a bit.
number ^= 1UL << n;
That will toggle the n
th bit of number
.
You didn't ask for this, but I might as well add it.
To check a bit, shift the number n to the right, then bitwise AND it:
bit = (number >> n) & 1U;
That will put the value of the n
th bit of number
into the variable bit
.
Setting the n
th bit to either 1
or 0
can be achieved with the following on a 2's complement C++ implementation:
number ^= (x ^ number) & (1UL << n);
Bit n
will be set if x
is 1
, and cleared if x
is 0
. If x
has some other value, you get garbage. x = !!x
will booleanize it to 0 or 1.
To make this independent of 2's complement negation behaviour (where 1
has all bits set, unlike on a 1's complement or sign/magnitude C++ implementation), use unsigned negation.
number ^= ((unsigned long)x ^ number) & (1UL << n);
or
unsigned long newbit = !!x; // Also booleanize to force 0 or 1
number ^= (newbit ^ number) & (1UL << n);
It's generally a good idea to use unsigned types for portable bit manipulation.
or
number = (number & ~(1UL << n))  (x << n);
(number & ~(1UL << n))
will clear the n
th bit and (x << n)
will set the n
th bit to x
.
It's also generally a good idea to not to copy/paste code in general and so many people use preprocessor macros (like the community wiki answer further down) or some sort of encapsulation.
Here's my favorite bit arithmetic macro, which works for any type of unsigned integer array from unsigned char
up to size_t
(which is the biggest type that should be efficient to work with):
#define BITOP(a,b,op) \
((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a)))))
To set a bit:
BITOP(array, bit, =);
To clear a bit:
BITOP(array, bit, &=~);
To toggle a bit:
BITOP(array, bit, ^=);
To test a bit:
if (BITOP(array, bit, &)) ...
etc.
Here is a routine in C to perform the basic bitwise operations:
#define INT_BIT (unsigned int) (sizeof(unsigned int) * 8U) //number of bits in unsigned int
int main(void)
{
unsigned int k = 5; //k is the bit position; here it is the 5th bit from the LSb (0th bit)
unsigned int regA = 0x00007C7C; //we perform bitwise operations on regA
regA = (1U << k); //Set kth bit
regA &= ~(1U << k); //Clear kth bit
regA ^= (1U << k); //Toggle kth bit
regA = (regA << k)  regA >> (INT_BIT  k); //Rotate left by k bits
regA = (regA >> k)  regA << (INT_BIT  k); //Rotate right by k bits
return 0;
}
Sometimes when you are not sure what 1 or the like will result in, you may wish to set the nth bit without using 1:
number = (((number  (1 << n)) ^ (1 << n)))  (x << n);
Explanation: ((number  (1 << n)
sets the nth bit to 1 (where 
denotes bitwise OR), then with (...) ^ (1 << n)
we set the nth bit to 0, and finally with (...)  x << n)
we set the nth bit that was 0, to (bit value) x
.
This also works in golang
.
Visual C 2010, and perhaps many other compilers, have direct support for boolean operations built in. A bit has two possible values, just like a boolean, so we can use booleans instead  even if they take up more space than a single bit in memory in this representation. This works, even the sizeof()
operator works properly.
bool IsGph[256], IsNotGph[256];
// Initialize boolean array to detect printable characters
for(i=0; i<sizeof(IsGph); i++) {
IsGph[i] = isgraph((unsigned char)i);
}
So, to your question, IsGph[i] =1
, or IsGph[i] =0
make setting and clearing bools easy.
To find unprintable characters:
// Initialize boolean array to detect UNprintable characters,
// then call function to toggle required bits true, while initializing a 2nd
// boolean array as the complement of the 1st.
for(i=0; i<sizeof(IsGph); i++) {
if(IsGph[i]) {
IsNotGph[i] = 0;
} else {
IsNotGph[i] = 1;
}
}
Note there is nothing "special" about this code. It treats a bit like an integer  which technically, it is. A 1 bit integer that can hold 2 values, and 2 values only.
I once used this approach to find duplicate loan records, where loan_number was the ISAM key, using the 6digit loan number as an index into the bit array. Savagely fast, and after 8 months, proved that the mainframe system we were getting the data from was in fact malfunctioning. The simplicity of bit arrays makes confidence in their correctness very high  vs a searching approach for example.
Here are some macros I use:
SET_FLAG(Status, Flag) ((Status) = (Flag))
CLEAR_FLAG(Status, Flag) ((Status) &= ~(Flag))
INVALID_FLAGS(ulFlags, ulAllowed) ((ulFlags) & ~(ulAllowed))
TEST_FLAGS(t,ulMask, ulBit) (((t)&(ulMask)) == (ulBit))
IS_FLAG_SET(t,ulMask) TEST_FLAGS(t,ulMask,ulMask)
IS_FLAG_CLEAR(t,ulMask) TEST_FLAGS(t,ulMask,0)
If you're doing a lot of bit twiddling you might want to use masks which will make the whole thing quicker. The following functions are very fast and are still flexible (they allow bit twiddling in bit maps of any size).
const unsigned char TQuickByteMask[8] =
{
0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80,
};
/** Set bit in any sized bit mask.
*
* @return none
*
* @param bit  Bit number.
* @param bitmap  Pointer to bitmap.
*/
void TSetBit( short bit, unsigned char *bitmap)
{
short n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] = TQuickByteMask[n]; // Set bit.
}
/** Reset bit in any sized mask.
*
* @return None
*
* @param bit  Bit number.
* @param bitmap  Pointer to bitmap.
*/
void TResetBit( short bit, unsigned char *bitmap)
{
short n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] &= (~TQuickByteMask[n]); // Reset bit.
}
/** Toggle bit in any sized bit mask.
*
* @return none
*
* @param bit  Bit number.
* @param bitmap  Pointer to bitmap.
*/
void TToggleBit( short bit, unsigned char *bitmap)
{
short n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] ^= TQuickByteMask[n]; // Toggle bit.
}
/** Checks specified bit.
*
* @return 1 if bit set else 0.
*
* @param bit  Bit number.
* @param bitmap  Pointer to bitmap.
*/
short TIsBitSet( short bit, const unsigned char *bitmap)
{
short n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
// Test bit (logigal AND).
if (bitmap[x] & TQuickByteMask[n])
return 1;
return 0;
}
/** Checks specified bit.
*
* @return 1 if bit reset else 0.
*
* @param bit  Bit number.
* @param bitmap  Pointer to bitmap.
*/
short TIsBitReset( short bit, const unsigned char *bitmap)
{
return TIsBitSet(bit, bitmap) ^ 1;
}
/** Count number of bits set in a bitmap.
*
* @return Number of bits set.
*
* @param bitmap  Pointer to bitmap.
* @param size  Bitmap size (in bits).
*
* @note Not very efficient in terms of execution speed. If you are doing
* some computationally intense stuff you may need a more complex
* implementation which would be faster (especially for big bitmaps).
* See (http://graphics.stanford.edu/~seander/bithacks.html).
*/
int TCountBits( const unsigned char *bitmap, int size)
{
int i, count = 0;
for (i=0; i<size; i++)
if (TIsBitSet(i, bitmap))
count++;
return count;
}
Note, to set bit 'n' in a 16 bit integer you do the following:
TSetBit( n, &my_int);
It's up to you to ensure that the bit number is within the range of the bit map that you pass. Note that for little endian processors that bytes, words, dwords, qwords, etc., map correctly to each other in memory (main reason that little endian processors are 'better' than bigendian processors, ah, I feel a flame war coming on...).
It is sometimes worth using an enum
to name the bits:
enum ThingFlags = {
ThingMask = 0x0000,
ThingFlag0 = 1 << 0,
ThingFlag1 = 1 << 1,
ThingError = 1 << 8,
}
Then use the names later on. I.e. write
thingstate = ThingFlag1;
thingstate &= ~ThingFlag0;
if (thing & ThingError) {...}
to set, clear and test. This way you hide the magic numbers from the rest of your code.
Other than that, I endorse Paige Ruten's solution.
For the beginner I would like to explain a bit more with an example:
Example:
value is 0x55;
bitnum : 3rd.
The &
operator is used check the bit:
0101 0101
&
0000 1000
___________
0000 0000 (mean 0: False). It will work fine if the third bit is 1 (then the answer will be True)
Toggle or Flip:
0101 0101
^
0000 1000
___________
0101 1101 (Flip the third bit without affecting other bits)

operator: set the bit
0101 0101

0000 1000
___________
0101 1101 (set the third bit without affecting other bits)
Try one of these functions in the C language to change n bit:
char bitfield;
// Start at 0th position
void chang_n_bit(int n, int value)
{
bitfield = (bitfield  (1 << n)) & (~( (1 << n) ^ (value << n) ));
}
Or
void chang_n_bit(int n, int value)
{
bitfield = (bitfield  (1 << n)) & ((value << n)  ((~0) ^ (1 << n)));
}
Or
void chang_n_bit(int n, int value)
{
if(value)
bitfield = 1 << n;
else
bitfield &= ~0 ^ (1 << n);
}
char get_n_bit(int n)
{
return (bitfield & (1 << n)) ? 1 : 0;
}
Use this:
int ToggleNthBit ( unsigned char n, int num )
{
if(num & (1 << n))
num &= ~(1 << n);
else
num = (1 << n);
return num;
}
The other option is to use bit fields:
struct bits {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
};
struct bits mybits;
defines a 3bit field (actually, it's three 1bit felds). Bit operations now become a bit (haha) simpler:
To set or clear a bit:
mybits.b = 1;
mybits.c = 0;
To toggle a bit:
mybits.a = !mybits.a;
mybits.b = ~mybits.b;
mybits.c ^= 1; /* all work */
Checking a bit:
if (mybits.c) //if mybits.c is non zero the next line below will execute
This only works with fixedsize bit fields. Otherwise you have to resort to the bittwiddling techniques described in previous posts.
The bitfield approach has other advantages in the embedded arena. You can define a struct that maps directly onto the bits in a particular hardware register.
struct HwRegister {
unsigned int errorFlag:1; // onebit flag field
unsigned int Mode:3; // threebit mode field
unsigned int StatusCode:4; // fourbit status code
};
struct HwRegister CR3342_AReg;
You need to be aware of the bit packing order  I think it's MSB first, but this may be implementationdependent. Also, verify how your compiler handlers fields crossing byte boundaries.
You can then read, write, test the individual values as before.
How do you set, clear, and toggle a single bit?
To address a common coding pitfall when attempting to form the mask:
1
is not always wide enough
What problems happen when number
is a wider type than 1
?
x
may be too great for the shift 1 << x
leading to undefined behavior (UB). Even if x
is not too great, ~
may not flip enough mostsignificantbits.
// assume 32 bit int/unsigned
unsigned long long number = foo();
unsigned x = 40;
number = (1 << x); // UB
number ^= (1 << x); // UB
number &= ~(1 << x); // UB
x = 10;
number &= ~(1 << x); // Wrong mask, not wide enough
To insure 1 is wide enough:
Code could use 1ull
or pedantically (uintmax_t)1
and let the compiler optimize.
number = (1ull << x);
number = ((uintmax_t)1 << x);
Or cast  which makes for coding/review/maintenance issues keeping the cast correct and uptodate.
number = (type_of_number)1 << x;
Or gently promote the 1
by forcing a math operation that is as least as wide as the type of number
.
number = (number*0 + 1) << x;
As with most bit manipulations, best to work with unsigned types rather than signed ones
#define bit_test(x, y) ( ( ((const char*)&(x))[(y)>>3] & 0x80 >> ((y)&0x07)) >> (7((y)&0x07) ) )
Sample usage:
int main(void)
{
unsigned char arr[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
for (int ix = 0; ix < 64; ++ix)
printf("bit %d is %d\n", ix, bit_test(arr, ix));
return 0;
}
Notes: This is designed to be fast (given its flexibility) and nonbranchy. It results in efficient SPARC machine code when compiled Sun Studio 8; I've also tested it using MSVC++ 2008 on amd64. It's possible to make similar macros for setting and clearing bits. The key difference of this solution compared with many others here is that it works for any location in pretty much any type of variable.
Using the Standard C++ Library: std::bitset<N>
.
Or the Boost version: boost::dynamic_bitset
.
There is no need to roll your own:
#include <bitset>
#include <iostream>
int main()
{
std::bitset<5> x;
x[1] = 1;
x[2] = 0;
// Note x[04] valid
std::cout << x << std::endl;
}
[Alpha:] > ./a.out
00010
The Boost version allows a runtime sized bitset compared with a standard library compiletime sized bitset.
This program is to change any data bit from 0 to 1 or 1 to 0:
{
unsigned int data = 0x000000F0;
int bitpos = 4;
int bitvalue = 1;
unsigned int bit = data;
bit = (bit>>bitpos)&0x00000001;
int invbitvalue = 0x00000001&(~bitvalue);
printf("%x\n",bit);
if (bitvalue == 0)
{
if (bit == 0)
printf("%x\n", data);
else
{
data = (data^(invbitvalue<<bitpos));
printf("%x\n", data);
}
}
else
{
if (bit == 1)
printf("elseif %x\n", data);
else
{
data = (data(bitvalue<<bitpos));
printf("else %x\n", data);
}
}
}
Expanding on the bitset
answer:
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main() {
bitset<8> byte(std::string("10010011");
// Set Bit
byte.set(3); // 10010111
// Clear Bit
byte.reset(2); // 10010101
// Toggle Bit
byte.flip(7); // 00010101
cout << byte << endl;
return 0;
}
Let suppose few things first
num = 55
Integer to perform bitwise operations (set, get, clear, toggle).
n = 4
0 based bit position to perform bitwise operations.
nth
bit of num right shift num
, n
times. Then perform bitwise AND &
with 1.bit = (num >> n) & 1;
How it works?
0011 0111 (55 in decimal)
>> 4 (right shift 4 times)

0000 0011
& 0000 0001 (1 in decimal)

=> 0000 0001 (final result)
n
times. Then perform bitwise OR 
operation with num
.num = (1 << n); // Equivalent to; num = (1 << n)  num;
How it works?
0000 0001 (1 in decimal)
<< 4 (left shift 4 times)

0001 0000
 0011 0111 (55 in decimal)

=> 0001 0000 (final result)
n
times i.e. 1 << n
.~ (1 << n)
.&
operation with the above result and num
. The above three steps together can be written as num & (~ (1 << n))
;num &= (~(1 << n)); // Equivalent to; num = num & (~(1 << n));
How it works?
0000 0001 (1 in decimal)
<< 4 (left shift 4 times)

~ 0001 0000

1110 1111
& 0011 0111 (55 in decimal)

=> 0010 0111 (final result)
To toggle a bit we use bitwise XOR ^
operator. Bitwise XOR operator evaluates to 1 if corresponding bit of both operands are different, otherwise evaluates to 0.
Which means to toggle a bit, we need to perform XOR operation with the bit you want to toggle and 1.
num ^= (1 << n); // Equivalent to; num = num ^ (1 << n);
How it works?
0 ^ 1 => 1
. 1 ^ 1 => 0
. 0000 0001 (1 in decimal)
<< 4 (left shift 4 times)

0001 0000
^ 0011 0111 (55 in decimal)

=> 0010 0111 (final result)
Recommended reading  Bitwise operator exercises
How to set, clear, and toggle a bit?