0 votes
in C Plus Plus by
How do you set, clear, and toggle a single bit?

1 Answer

0 votes
by

Setting a bit

Use the bitwise OR operator (|) to set a bit.

number |= 1UL << n;

That will set the nth bit of numbern should be zero, if you want to set the 1st bit and so on upto n-1, if you want to set the nth 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.

Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

<code style="margin: 0px; padding: 0px; border: 0px; font-st

Related questions

0 votes
asked Jan 6, 2021 in C Plus Plus by GeorgeBell
0 votes
asked Jan 6 in C Plus Plus by GeorgeBell
...