Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.math.BigInteger; // import BigInteger method
- public class ShiftBits {
- public static void main (String [] args) {
- Scanner in = new Scanner(System.in);
- System.out.print("Enter number of bits: ");
- int bi = in.nextInt();
- int [] a = new int [bi]; // create array to keep binary number that you have to input
- for (int q = 0 ; q < a.length ; q++)
- {
- System.out.printf("Insert your %d position bit", q);
- int d = in.nextInt();
- a[q] = d; // add bit to the array
- }
- String b = getBi(a); // call method
- printRe(b); // call method to print
- System.out.println("Do you want to shift?\nIf yes and shift right say 1\nIf no say 0\nIf yes and shift left say -1"); // ask
- int u = in.nextInt();
- switch (u) { // use this instead of IF
- case 0: System.out.print("Thank you for playing");
- break; // if you forget to write this all of the case will be executed
- case 1: System.out.print("What times you want to shift?: ");
- int v = in.nextInt();
- b = shiftR(b, v);
- printRe(b);
- break;
- case -1: System.out.print("What times you want to shift?: ");
- int k = in.nextInt();
- b = shiftL(b, k);
- printRe(b);
- break;
- }
- }
- public static String getBi(int[] l) {
- String h = "";
- for (int q = 0 ; q < l.length ; q++)
- {
- h = h + l[q];
- }
- return h;
- }
- public static String printRe(String n) {
- System.out.printf("Your Binary is: %s\n", n);
- int e = Integer.parseInt(n, 2); // change to decimal
- System.out.printf("Your Decimal value is: %d\n", e);
- String r = new BigInteger(n, 2).toString(16).toUpperCase(); // change to Hexadecimal and change to uppercase
- System.out.printf("Your Hexacimal value is: %s\n", r);
- return n;
- }
- public static String shiftR(String i, int d) {
- String x = i;
- for (int q = 0 ; q < d ; q++)
- {
- x = x + 0;
- }
- return x;
- }
- public static String shiftL(String i, int d) {
- String z = i;
- for (int q = 0 ; q < d ; q++)
- {
- z = 0 + z;
- }
- return z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement