本文介绍了如何将其反转(字符串bindata ="110000")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C语言中将二进制数据11000转换为尖峰"string bindata =" 110000"....转换后应该为001111

how the binary data 11000 can be inverted in c sharp " string bindata = "110000"....after inverting it should come 001111

推荐答案

int number = Convert.ToInt32(binary_data,2);
String inverted_binary_data = Convert.ToString (!number , 2);


祝你好运!


Good luck!


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BitWiseComplement("11000");
            BitWiseComplement("00111111111111");
            BitWiseComplement("11000000000000");
            BitWiseComplement("11111111100000");
            BitWiseComplement("11111100001111");
        }

        static void BitWiseComplement(string firstBinaryNumber)
        {
            int result = Convert.ToInt32(firstBinaryNumber, 2);
            Console.WriteLine("{0,5}\t{1}", result, firstBinaryNumber);
            string complementedBinaryNumber = Convert.ToString(~result, 2);
            complementedBinaryNumber = complementedBinaryNumber.Remove(0, complementedBinaryNumber.Length - firstBinaryNumber.Length);
            Console.WriteLine("{0,5}\t{1}\n", Convert.ToInt32(complementedBinaryNumber, 2), complementedBinaryNumber);
        }
    }
}



根据我使用的测试数据,它将产生以下输出,



Which will produce following output based on the test data I used,

   24   11000
    7   00111

 4095   00111111111111
12288   11000000000000

12288   11000000000000
 4095   00111111111111

16352   11111111100000
   31   00000000011111

16143   11111100001111
  240   00000011110000



希望对您有所帮助:)



Hope it helps :)


string binData = "110000";
string invertedData = binData.Replace('0','*').Replace('1','0').Replace('*','1');




或按以下方式使用LINQ




or using LINQ as below

string binData = "110000";
string invertedData = new string(binData.Select(ch => ch == '0' ? '1' : '0').ToArray());


这篇关于如何将其反转(字符串bindata ="110000")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:34