Reputation: 535
Say for instance, you have an 8-character string representing an 8-bit byte; i.e '00000000'
(0) and you want to flip a single bit, to make it '00010000'
(16). What's the best or most elegant way to do it?
Upvotes: 0
Views: 2824
Reputation: 1041
When speaking about bit manipulation, classical way to flip single bit at nth position is
x ^= 1 << n
XOR 1 always flips the bit. But if you use strings, then every character is not bit, but the whole byte. So you can try string-to-int conversion, using XOR 1 and inserting new symbol back to the string. Or just using if-else statement.
Upvotes: 1