Reputation: 9
Here is the scenario, I have a list of numbers both positive and negatives. And another list of bits(0's and 1's). I have to replace these bits to the LSB's of every number in the 1st list (i.e:- of numbers).
for (int k = 0; k< 8; k++)
{
i1 = index[k, 0];
i2 = index[k, 1];
string str1 = Convert.ToString(d[i1,i2], 2);
str1 = str1.Substring(Math.Max(str1.Length - 8, 0)).PadLeft(8, '0');
str1[0] = Tnbinary[k];
}
It won't allow me to modify str[0] - it says that it is only readable. What am I doing wrong and how can I modify my number?
Upvotes: 0
Views: 940
Reputation: 54897
You could use bit operations:
byte b = -3;
byte b0 = (byte)(b & 254); // Make LSB 0
byte b1 = (byte)(b | 1); // Make LSB 1
If you're using signed bytes, just replace byte
with sbyte
.
Upvotes: 3
Reputation: 116367
If this is really a byte, why you don't do bit arithmetic, something like this:
new_byte = (old_byte & ~1) | new_bit
Upvotes: 1