Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Change Even Bits
- //You are given a number N and you have to read from the console exactly N + 1 integers and additionally an integer L – the number //to be processed. Your task is to count the bits in each of the N input integers (let this be the number len), then change bit to //"1" len even positions (0, 2, 4, …) of the input number L. Print on the console the obtained number and the total number of bits //that have actually been changed. To be counted as changed they should have been “0” at first and then changed to “1”. The //comments in the example below will help you understand better what you should do. Note that "0" consists of 1 bit (len=1).
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[] numbers = new int[n];
- int[] counts = new int[n];
- int count = 0;
- int f = 0;
- for (int i = 0; i < n; i++)
- {
- int num = int.Parse(Console.ReadLine());
- string bin = Convert.ToString(num, 2);
- count = bin.Length;
- numbers[f] = num;
- counts[f] = count;
- f++;
- }
- int bitCounts = 0;
- int l = int.Parse(Console.ReadLine());
- for (int k = 0; k < n; k++)
- {
- for (int j = 0; j < 2 * counts[k] - 1; j += 2)
- {
- int max = 2 * counts[k] - 1;
- if (max < counts[k])
- {
- max = counts[k];
- }
- string b = Convert.ToString(l, 2).PadRight(max, '0');
- int bitValue = l & (1 << j);
- if (bitValue == 0)
- {
- bitCounts++;
- }
- l = l | (1 << j);
- }
- }
- Console.WriteLine(l);
- Console.WriteLine(bitCounts);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement