Advertisement
dimipan80

Exam 9. Friend Bits

Jun 24th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. namespace _5.FriendBits
  2. {
  3.     using System;
  4.  
  5.     public class FriendBits
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 uint num = uint.Parse(Console.ReadLine());
  12.  
  13.                 uint friendBits = 0;
  14.                 uint aloneBits = 0;
  15.                 for (int i = 31; i >= 0; i--)
  16.                 {
  17.                     uint currentBitValue = (num >> i) & 1;
  18.                     uint leftBitValue = (num >> (i + 1)) & 1;
  19.                     uint rightBitValue = (num >> (i - 1)) & 1;
  20.  
  21.                     if ((i < 31 && currentBitValue == leftBitValue) || (i > 0 && currentBitValue == rightBitValue))
  22.                     {
  23.                         friendBits = (friendBits << 1) | currentBitValue;
  24.                     }
  25.                     else
  26.                     {
  27.                         aloneBits = (aloneBits << 1) | currentBitValue;
  28.                     }
  29.                 }
  30.  
  31.                 Console.WriteLine(friendBits);
  32.                 Console.WriteLine(aloneBits);
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement