Advertisement
dimipan80

Exam 5. Bit Flipper (on Java code)

Sep 10th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class _5_BitFlipper {
  5.  
  6.     private static final BigInteger SEVEN = new BigInteger("7");
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner scan = new Scanner(System.in);
  11.         BigInteger number = scan.nextBigInteger();
  12.  
  13.         BigInteger bitMask;
  14.         BigInteger bitValue;
  15.         for (int position = 63; position >= 2; position--) {
  16.             bitMask = SEVEN.shiftLeft(position - 2);
  17.             bitValue = number.and(bitMask);
  18.             bitValue = bitValue.shiftRight(position - 2);
  19.             if (bitValue.equals(BigInteger.ZERO)) {
  20.                 number = number.or(bitMask);
  21.                 position -= 2;
  22.             } else if (bitValue.equals(SEVEN)) {
  23.                 number = number.andNot(bitMask);
  24.                 position -= 2;
  25.             }
  26.         }
  27.  
  28.         System.out.println(number);
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement