stefan1919

Change Even Bits

Aug 18th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 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.  
  7. namespace ConsoleApplication2
  8. {
  9.     class Program
  10.     {
  11.        
  12.         static void Main(string[] args)
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.             int number = 0;
  16.             int changed = 0;
  17.             List<int> counter = new List<int>();
  18.             for (int i = 0; i < n; i++)
  19.             {
  20.                 number = int.Parse(Console.ReadLine());
  21.                 string binary = Convert.ToString(number, 2);
  22.                 int positions = binary.Length;
  23.                 counter.Add(positions);
  24.             }
  25.             int l = int.Parse(Console.ReadLine());
  26.             foreach (var item in counter)
  27.             {
  28.                 for (int j = 0; j <= 2 * item - 2; j += 2)
  29.             {
  30.                 int mask = 1 << j;
  31.  
  32.                 int bitValue = (l & mask) >> j;
  33.                 if (bitValue == 0)
  34.                {
  35.                     int maskForConversion = (1 << j);
  36.                     l = l | maskForConversion;
  37.                     changed++;
  38.                 }
  39.  
  40.             }
  41.  
  42.             }
  43.             Console.WriteLine(l);
  44.             Console.WriteLine(changed);
  45.            
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment