Changing the nth bit to x

suggest change

C-style bit-manipulation

// Bit n will be set if x is 1 and cleared if x is 0.
number ^= (-x ^ number) & (1LL << n);

Using std::bitset

set(n,val) - sets bit n to the value val.

std::bitset<5> num(std::string("00100"));
num.set(0,true);  // num is now 00101
num.set(2,false); // num is now 00001

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Bit manipulation:
* Changing the nth bit to x

Table Of Contents
6 Bit manipulation
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype