Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
how the binary data 11000 can be inverted in c sharp " string bindata = "110000"....after inverting it should come 001111
Posted
Comments
[no name] 20-Apr-12 7:21am    
If you invert (reverse) "110000" it will become "000011". Where is it that you expect the other "1" to come from to make it "001111"?
Rahul_Chand 20-Apr-12 8:10am    
sorry i meant to compliment the binary no like 1 to 0 or vice -versa
lukeer 20-Apr-12 9:30am    
What's wrong with the answers to your previous identical question[^]?
Sergey Alexandrovich Kryukov 22-Apr-12 20:49pm    
Bad idea! You should work with data, not string representation of data.
--SA

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

Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Apr-12 20:52pm    
This is better, my 5. However, I would write a note to OP: most important thing here is to point out that working with stings representing data is wrong. One should work with data itself, never do any operations on strings.
--SA
It might help,

C#
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 :)
 
Share this answer
 
Comments
VJ Reddy 20-Apr-12 10:30am    
Nice answer. 5!
Mohammad A Rahman 20-Apr-12 10:39am    
Thanks VJ :)
Nelek 20-Apr-12 17:21pm    
+5
Mohammad A Rahman 20-Apr-12 17:55pm    
Thanks Nelek :)
If it is meant to replace 0 with 1 and 1 with 0, then the Replace method of string class can be used as below
C#
string binData = "110000";
string invertedData = binData.Replace('0','*').Replace('1','0').Replace('*','1');



or using LINQ as below

C#
string binData = "110000";
string invertedData = new string(binData.Select(ch => ch == '0' ? '1' : '0').ToArray());
 
Share this answer
 
v4
Comments
Rahul_Chand 20-Apr-12 8:08am    
can u please tell me that in your first answer where did u mention the bindata in string.replace method.
VJ Reddy 20-Apr-12 10:26am    
Sorry! Inadvertently, I have written string.Replace, instead of binData.Replace. Please see my updated solution.
Thank you for the reponse.
Sergey Alexandrovich Kryukov 22-Apr-12 20:51pm    
Well, a vote of 1 someone gave you is not fair; I voted 4.
The problem is: most important thing here is to point out that working with stings representing data is wrong. One should work with data itself, never do any operations on strings.
--SA
VJ Reddy 22-Apr-12 21:15pm    
Thanks a lot, SA, for the vote. One should work with data itself is really an important point. Thank you for suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900