BorislavBorisov

04.02.Bit Flipper-Авторско побитови операции

Oct 10th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using System;
  2. /*Обръщане на няколко поредни (еднакви) бита се прави с маска*/
  3. class ChangeBits
  4. {
  5.     static void Main()
  6.     {
  7.         ulong N = ulong.Parse(Console.ReadLine());
  8.         //int bits = 62;
  9.         int bits = 61;
  10.         while(bits >= 0)
  11.         {
  12.             //bits--;
  13.             ulong last3Bits = (N >> bits) & 7;//буташ ги и ги сравняваш със 111
  14.             if(last3Bits == 0 || last3Bits == 7)
  15.             {
  16.                 N ^= ((ulong)7 << bits);
  17.                 bits -= 2;
  18.             }
  19.             bits--;
  20.         }
  21.         Console.WriteLine(N);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment