bit manipulation - How to flip bits in C#? -
i have binary number in form of string as
string input = "10110101101";
now need flip (0 1
, 1 0
) first 3 bits of it.
the resultant output 01010101101
how in c#?
an alternate way using convert.toint32
, convert.tostring
(which otherwise unknown , unused), , bitwise-xor
string input = "10110101101"; int flipno = 3; int output = convert.toint32(input, 2); (int = input.length - 1; >= input.length - flipno; --i) output ^= 1 << i;
simply use output
, or if want display output
in string
, do:
string display = convert.tostring(output, 2).padleft(input.length, '0');
Comments
Post a Comment