Advertisement
dimipan80

C#Exams 5. Bit Roller (on Java Code)

Aug 23rd, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _5_BitRoller {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         Scanner scan = new Scanner(System.in);
  8.         int number = scan.nextInt();
  9.         int frozenBit = scan.nextInt();
  10.         int movingTimes = scan.nextInt();
  11.  
  12.         movingTimes %= 18;
  13.         for (int i = 0; i < movingTimes; i++) {
  14.             number = rollBitsNumberOneTimeToTheRight(number, frozenBit);
  15.         }
  16.  
  17.         System.out.println(number);
  18.     }
  19.  
  20.     private static int rollBitsNumberOneTimeToTheRight(int number, int frozenBit) {
  21.         // TODO Auto-generated method stub
  22.         int resultNum = 0;
  23.         for (int bit = 0; bit < 19; bit++) {
  24.             int bitValue = (number >>> bit) & 1;
  25.             if (bit == frozenBit) {
  26.                 resultNum |= bitValue << bit;
  27.             } else {
  28.                 int nextPosition = moveAtRightPosition(frozenBit, bit);
  29.                 resultNum |= bitValue << nextPosition;
  30.             }
  31.         }
  32.  
  33.         return resultNum;
  34.     }
  35.  
  36.     private static int moveAtRightPosition(int frozenBit, int bit) {
  37.         // TODO Auto-generated method stub
  38.         int nextPosition = bit - 1;
  39.         if (nextPosition < 0) {
  40.             nextPosition = 18;
  41.         }
  42.  
  43.         if (nextPosition == frozenBit) {
  44.             nextPosition -= 1;
  45.             if (nextPosition < 0) {
  46.                 nextPosition = 18;
  47.             }
  48.         }
  49.         return nextPosition;
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement