Advertisement
dimipan80

Exam 7. Bit Shooter

Jun 21st, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4.  
  5. public class BitShooter
  6. {
  7.     public static void Main()
  8.     {
  9.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  10.         checked
  11.         {
  12.             ulong number = ulong.Parse(Console.ReadLine());
  13.  
  14.             int leftBits = 0;
  15.             int rightBits = 0;
  16.             if (number > 0)
  17.             {                
  18.                 for (int i = 0; i < 3; i++)
  19.                 {
  20.                     string[] inputStr = Console.ReadLine().Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
  21.                     int center = int.Parse(inputStr[0]);
  22.                     int size = int.Parse(inputStr[1]);
  23.                     int damageMinBitPosition = center - (size / 2);
  24.                     if (damageMinBitPosition < 0)
  25.                     {
  26.                         damageMinBitPosition = 0;
  27.                     }
  28.  
  29.                     int damageMaxBitPosition = center + (size / 2);
  30.                     if (damageMaxBitPosition > 63)
  31.                     {
  32.                         damageMaxBitPosition = 63;
  33.                     }
  34.  
  35.                     int countBitsToChange = (damageMaxBitPosition - damageMinBitPosition) + 1;        
  36.                     ulong bitMask = ((ulong)1 << countBitsToChange) - 1;
  37.                     bitMask <<= damageMinBitPosition;
  38.                     number &= ~bitMask;
  39.                 }
  40.  
  41.                 for (int bits = 0; bits < 64; bits++)
  42.                 {                    
  43.                     int bitValue = (int)(number & 1);
  44.                     if (bits >= 32 && bitValue > 0)
  45.                     {
  46.                         leftBits++;
  47.                     }
  48.                     else if (bits < 32 && bitValue > 0)
  49.                     {
  50.                         rightBits++;
  51.                     }
  52.  
  53.                     if (number == 0)
  54.                     {
  55.                         break;
  56.                     }
  57.  
  58.                     number >>= 1;
  59.                 }
  60.             }
  61.  
  62.             Console.WriteLine("left: {0}\nright: {1}", leftBits, rightBits);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement