fbinnzhivko

05.00 FriendBits

Apr 15th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2. class FriendBits
  3. {
  4.     static void Main()
  5.     {
  6.         uint n = uint.Parse(Console.ReadLine());
  7.         uint friendBits = 0;
  8.         uint aloneBits = 0;
  9.         for (int i = 31; i >= 0; i--)
  10.         {
  11.             uint currentBit = (n >> i) & 1;
  12.  
  13.             bool hasLeftBit = (i < 31);
  14.             uint leftBit = (n >> (i + 1)) & 1;
  15.             bool sameLeftBit = hasLeftBit && (leftBit == currentBit);
  16.  
  17.             bool hasRightBit = (i > 0);
  18.             uint rightBit = (n >> (i - 1)) & 1;
  19.             bool sameRightBit = hasRightBit && (rightBit == currentBit);
  20.  
  21.             bool friendBit = sameLeftBit || sameRightBit;
  22.             if (friendBit)
  23.             {
  24.                 friendBits = (friendBits << 1) | currentBit;
  25.             }
  26.             else
  27.             {
  28.                 aloneBits = (aloneBits << 1) | currentBit;
  29.             }
  30.         }
  31.         Console.WriteLine(friendBits);
  32.         Console.WriteLine(aloneBits);
  33.     }
  34. }
Add Comment
Please, Sign In to add comment