Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class _5_BitRoller {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- int number = scan.nextInt();
- int frozenBit = scan.nextInt();
- int movingTimes = scan.nextInt();
- movingTimes %= 18;
- for (int i = 0; i < movingTimes; i++) {
- number = rollBitsNumberOneTimeToTheRight(number, frozenBit);
- }
- System.out.println(number);
- }
- private static int rollBitsNumberOneTimeToTheRight(int number, int frozenBit) {
- // TODO Auto-generated method stub
- int resultNum = 0;
- for (int bit = 0; bit < 19; bit++) {
- int bitValue = (number >>> bit) & 1;
- if (bit == frozenBit) {
- resultNum |= bitValue << bit;
- } else {
- int nextPosition = moveAtRightPosition(frozenBit, bit);
- resultNum |= bitValue << nextPosition;
- }
- }
- return resultNum;
- }
- private static int moveAtRightPosition(int frozenBit, int bit) {
- // TODO Auto-generated method stub
- int nextPosition = bit - 1;
- if (nextPosition < 0) {
- nextPosition = 18;
- }
- if (nextPosition == frozenBit) {
- nextPosition -= 1;
- if (nextPosition < 0) {
- nextPosition = 18;
- }
- }
- return nextPosition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement