简体   繁体   中英

Negative number binary and change LSB of it

Sir I have a problem.I find the binary of a negative number that gives me in 32 bits.I trim all other bits except the first 8 bits.Now I change LSB of it.It gives me 125 as answer. I have to embed this in an gray scale image. According to my requirement , I have to add this 125 in mean(sum of four neighbouring pixels) .When i add this to mean.I gives me answer that exceeds 255. So is it posible that after modifing lsb. my bit remain negative.

here is my code

string str1 = Convert.ToString(d[1, 1], 2);
str1 = str1.Substring(Math.Max(str1.Length - 8, 0)).PadLeft(8, '0');
char[] data = new char[str1.Length]; 

for (int m = 0; m < str1.Length; m++)
{
    data[m] = str1[m];
}

//data[0] = '0';
string s="";
data[0] = '0';

for (int m = 0; m < str1.Length; m++)
{
    s += data[m];
}

byte output = Convert.ToByte(s, 2);

Your code is trimming all but the last 8 bits. If you want to trim all but the first 8 bits, you should use str1.Substring(0, 8). And you are changing the MSB of the last 8 bits. if you want to change the LSB, you should use data[7] instead of data[0].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM