Filkolev

Bitwise 1024

Nov 2nd, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char** argv) {
  6.     int count_of_numbers;
  7.     scanf("%d", &count_of_numbers);
  8.  
  9.     unsigned long long numbers[count_of_numbers];
  10.  
  11.     int i;
  12.     for (i = 0; i < count_of_numbers; i++) {
  13.         scanf("%llu", &numbers[i]);
  14.     }
  15.  
  16.     while (1) {
  17.         char direction[6];
  18.         scanf("%s", direction);
  19.  
  20.         if (strcmp(direction, "end") == 0) {
  21.             break;
  22.         }
  23.  
  24.         unsigned long long start_position;
  25.         scanf("%llu", &start_position);
  26.  
  27.         unsigned long long mask = 1;
  28.         int number_index;
  29.         for (number_index = 0; number_index < count_of_numbers; number_index++) {
  30.             int bit_index;
  31.             int target_index;
  32.             int target_set = 0;
  33.  
  34.             if (strcmp(direction, "right") == 0) {
  35.                 for (bit_index = 0; bit_index <= start_position; bit_index++) {
  36.                     unsigned long long bit = (numbers[number_index] >> bit_index) & mask;                    
  37.                     if (bit == 1 && target_set == 1) {
  38.                         numbers[number_index] |= mask << target_index;
  39.                         numbers[number_index] &= ~(mask << bit_index);
  40.                         target_set = 0;
  41.                         bit_index = 0;
  42.                     } else if (bit == 0 && target_set == 0) {
  43.                         target_index = bit_index;
  44.                         target_set = 1;
  45.                     }
  46.                 }
  47.             } else if (strcmp(direction, "left") == 0) {
  48.                 for (bit_index = 63; bit_index >= start_position; bit_index--) {
  49.                     unsigned long long bit = (numbers[number_index] >> bit_index) & mask;                    
  50.                     if (bit == 1 && target_set == 1) {                        
  51.                         numbers[number_index] |= mask << target_index;                        
  52.                         numbers[number_index] &= ~(mask << bit_index);                        
  53.                         target_set = 0;
  54.                         bit_index = 63;
  55.                     } else if (bit == 0 && target_set == 0) {
  56.                         target_index = bit_index;
  57.                         target_set = 1;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63.  
  64.     for (i = 0; i < count_of_numbers; i++) {
  65.         printf("%llu\n", numbers[i]);
  66.     }
  67.  
  68.     return (EXIT_SUCCESS);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment