Int32进制转换

占8个字节,共32位,其中第1位为符号位.

范围:-2,147,483,648 到 2,147,483,647

补码的目的是正数负数直接计算,要求是绝对值相同的正数和负数相加等于0.

先假定用按位取反的方法(如00100111和11011000),则两数相加必然为11111111,此时再加一个1就全部是0了。

所以负数为正数按位取反再加一个1

View Code
 1 public static void Main()
 2     {
 3         string s1="00000000 00000000 00000000 00000001";        //十进制:1
 4         string s2="11111111 11111111 11111111 11111111";        //十进制:-1
 5         int i = Convert.ToInt32(s1.Replace(" ",""), 2);
 6         int j=Convert.ToInt32(s2.Replace(" ",""),2);
 7         Console.WriteLine("i:{0},j:{1} 两数和为:{2}",i,j,i+j);
 8         
 9         string s3="01111111 11111111 11111111 11111111";        //int最大值:2的31次方-1
10         string s4="10000000 00000000 00000000 00000001";
11         Console.WriteLine(Convert.ToInt32(s4.Replace(" ",""),2));
12         
13         string s5="00000000 00000000 00000000 00000000";        //int最小值:0
14         string s6="10000000 00000000 00000000 00000000";        //取反加1
15         Console.WriteLine("int最小值为:{0}",Convert.ToInt32(s6.Replace(" ",""),2));        
16     }
原文地址:https://www.cnblogs.com/goahead777/p/2465333.html