Advertisement
dimipan80

Exam July Evening 5. Change Even Bits

Jul 27th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. public class ChangeEvenBits
  4. {
  5.     public static void Main()
  6.     {        
  7.         checked
  8.         {
  9.             int count = int.Parse(Console.ReadLine());
  10.  
  11.             int[] nums = new int[count];
  12.             for (int i = 0; i < nums.Length; i++)
  13.             {
  14.                 nums[i] = int.Parse(Console.ReadLine());
  15.             }
  16.  
  17.             ulong modifyingNum = ulong.Parse(Console.ReadLine());
  18.  
  19.             int countModifyedBits = 0;
  20.             for (int i = 0; i < nums.Length; i++)
  21.             {
  22.                 string binaryNum = Convert.ToString(nums[i], 2);
  23.                 int countBits = binaryNum.Length;
  24.                 int maxEvenBit = (countBits - 1) * 2;
  25.                 for (int bit = 0; bit <= maxEvenBit; bit += 2)
  26.                 {
  27.                     int bitValue = (int)(modifyingNum & (ulong)(1 << bit));
  28.                     if (bitValue == 0)
  29.                     {
  30.                         countModifyedBits++;
  31.                         modifyingNum |= (ulong)1 << bit;
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine(modifyingNum);
  37.             Console.WriteLine(countModifyedBits);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement