Advertisement
d_brezoev

Inverting Bits

Nov 7th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. class InvertingBits
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         int n = 9;
  13.  
  14.         //number n as string
  15.         string nAsStr = Convert.ToString(n, 2);
  16.  
  17.         //declare empty string
  18.         string maskPrepare = string.Empty;
  19.  
  20.         //fill string with ones - "1111"
  21.         for (int i = 0; i < nAsStr.Length; i++)
  22.         {
  23.             maskPrepare += 1;
  24.         }
  25.  
  26.         //convert string to int
  27.         int mask = Convert.ToInt32(maskPrepare, 2);
  28.  
  29.         int result = n ^ mask;
  30.         Console.WriteLine(result);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement