NPSF3000

ZigZag

Apr 6th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ZigZag
  8. {
  9.     class Program
  10.     {
  11.         static Random rnd = new Random();
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             Parallel.For(int.MinValue, (long)int.MaxValue +1, i =>
  16.                 {
  17.                     int x = (int)i;
  18.                     uint zig = (uint)((x << 1) ^ (x >> 31));
  19.                     var zag = ((int)(zig >> 1)) ^ ((int)(-(zig & 1)));
  20.                     if (x != zag)
  21.                     {
  22.                         Console.WriteLine(x + " : " + zig + " : " + zag);
  23.                         Console.WriteLine(ToBits(x));
  24.                         Console.WriteLine(ToBits(zig));
  25.                         Console.WriteLine(ToBits(zag));
  26.                         Console.WriteLine("ERROR");
  27.                     }
  28.                 });
  29.             Console.WriteLine("Finished!");
  30.             Console.ReadLine();
  31.         }
  32.  
  33.         public static string ToBits(int i)
  34.         {
  35.             var bits = Convert.ToString(i, 2).PadLeft(32, '0');
  36.             return bits.Substring(0, 8) + " " + bits.Substring(8, 8) + " " + bits.Substring(16, 8) + " " + bits.Substring(24, 8);
  37.         }
  38.         public static string ToBits(uint i)
  39.         {
  40.             var bits = Convert.ToString(i, 2).PadLeft(32, '0');
  41.             return bits.Substring(0, 8) + " " + bits.Substring(8, 8) + " " + bits.Substring(16, 8) + " " + bits.Substring(24, 8);
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment