borkins

10. Diamond - bit wise operations

Apr 14th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. /**
  2.  * Project: Drawing_With_Loops - created by borkins on 2017-04-15
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _10_Diamond
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner console = new Scanner(System.in);
  12.  
  13.         int n = Integer.parseInt(console.nextLine()); // 0 <= n <= 63
  14.  
  15.         long[] arr = new long[(n > 1) ? (n - (n >> 1) - 1) : n];
  16.         long mid = (1L << n - 1) + 1;
  17.         long half = mid >> 1;
  18.         long add = 2;
  19.        
  20.         for (int i = arr.length - 1; i >= 0; i--)
  21.         {
  22.             if (i == 0 && n % 2 != 0) {
  23.                 arr[i] = half;
  24.                 break;
  25.             }
  26.  
  27.             arr[i] = half + add;
  28.             half >>= 1;
  29.             add <<= 1;
  30.         }
  31.        
  32.         for (int i = 0; i < arr.length; i++) drawLine(arr[i], n);
  33.        
  34.         if (n > 1) {
  35.             drawLine(mid, n);
  36.             for (int i = arr.length - 1; i >= 0; i--) drawLine(arr[i], n);
  37.         }
  38.     }
  39.  
  40.     public static void drawLine(long num, int count)
  41.     {
  42.         long mask = 1L;
  43.         for (int i = 0; i < count; i++) {
  44.             System.out.print((num & mask) == 0 ? "-" : "*");
  45.             mask <<= 1;
  46.         }
  47.         System.out.println();
  48.     }
  49. }
Add Comment
Please, Sign In to add comment