Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleTest
- {
- class ConsoleTest
- {
- static void Main()
- {
- Console.Title = "Bit Operations Exercise";
- int n = int.Parse(Console.ReadLine());
- byte v = byte.Parse(Console.ReadLine());
- int p = int.Parse(Console.ReadLine());
- string num = Convert.ToString(n,2).PadLeft(8,'0');
- Console.WriteLine(n+" in binary: " + num);
- char bitAtPos = num[num.Length-p-1];
- Console.WriteLine("bit at position "+ p +" --> " + bitAtPos);
- if (int.Parse(bitAtPos.ToString()) == (int)v)
- {
- Console.WriteLine(n + " (after its bit at position " + p + " was changed to " + v + ") is the same number --> " + n);
- }
- else
- {
- int mask = 1 << p;
- int finalNum = n ^ mask;
- Console.WriteLine(n + " (after its bit at position " + p + " was changed to " + v + ") is different number --> " + finalNum);
- Console.WriteLine(finalNum + " in binary: " + Convert.ToString(finalNum, 2).PadLeft(8, '0'));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment